在本地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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值