在本地client端创建一个指定端口的WCF service的方法

我们的新项目要我们在原有代码的基础上做bug fix和一些功能开发,我们拿到的代码里要调用一个本地的wcf service, 但是搜索了所有项目文件都没有找到类似于web项目的文件,当时就跟客户确认这个问题,后来客户回信说那个wcf service会自动地创建的,所以我们坚定了信心再研究研究其中的蹊跷,果不其然,创建一个本地client端的wcf service并不需要一个web project, 并且就几行代码,虽然这东西不复杂,但对我来说确实孤陋寡闻了,所以贴出来,供大家分享,提意见。

            try
            {
               Trace.WriteLine("Establishing the Media Command Service on port 8080...");
                this.serviceHost = new System.ServiceModel.ServiceHost(typeof(CommandServer), new Uri("http://"+Environment.MachineName+":8080"));
                BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Message);
               
                // Enable metadata publishing.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                //smb.HttpGetBinding = binding;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                this.serviceHost.Description.Behaviors.Add(smb);               
                this.serviceHost.Open();
                Trace.WriteLine("Command Service is ready and listening.");
            }
            catch (Exception e)
            {
                Trace.WriteLine("!! Error: " + e.ToString());

            }

 

        <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ICommandInterface" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8080/" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_ICommandInterface"
                contract="ServiceReference.ICommandInterface" name="BasicHttpBinding_ICommandInterface" />
        </client>
    </system.serviceModel>

你可能感兴趣的:(exception,service,Security,command,WCF,binding)