WCF服务的方法名固定,但是地址是动态的。怎么动态调用WCF服务?
就是说动态引用WCF服务。通过参数在运行时调用的改变WCF地址。
应该怎么做?

解决方案 »

  1.   

    wcf没搞过,不错webservice搞过,打开app.config
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="RTXServiceSoap" 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://172.18.238.252/rtxservice.asmx" binding="basicHttpBinding"
                    bindingConfiguration="RTXServiceSoap" contract="ServiceReference1.RTXServiceSoap"
                    name="RTXServiceSoap" />
            </client>
        </system.serviceModel>
    </configuration>
    修改ip地址就ok
      

  2.   

    当然可以 定义一个绑定方法 这里使用的是tcp,实际根据你的情况
     private static NetTcpBinding GetNetTcpBinding(bool TransactionFlow)
            {
                NetTcpBinding tcp = new NetTcpBinding();
                tcp.TransactionFlow = TransactionFlow;
                tcp.TransactionProtocol = System.ServiceModel.TransactionProtocol.WSAtomicTransactionOctober2004;
                tcp.CloseTimeout = new TimeSpan(0, 1, 0);
                tcp.OpenTimeout = new TimeSpan(0, 1, 0);
                tcp.ReceiveTimeout = new TimeSpan(0, 20, 0);
                tcp.SendTimeout = new TimeSpan(0, 1, 0);
                tcp.TransferMode = TransferMode.Buffered;
                tcp.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                tcp.MaxBufferPoolSize = int.MaxValue;
                tcp.MaxBufferSize = int.MaxValue;
                tcp.MaxReceivedMessageSize = int.MaxValue;
                tcp.ReaderQuotas.MaxArrayLength = int.MaxValue;
                tcp.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
                tcp.ReaderQuotas.MaxDepth = 32;
                tcp.ReaderQuotas.MaxNameTableCharCount = 16384;
                tcp.ReaderQuotas.MaxStringContentLength = int.MaxValue;
                tcp.MaxConnections = 10; //最大连接数
                tcp.ListenBacklog = 10;  //最大挂起
                return tcp;
            }  //定义一个终结点与地址:服务名需要根据发布的hex确定
       EndpointAddress endadd = new EndpointAddress("net.tcp://服务器址址:端口/服务名");
       
       //回调对象根据实际,有些没有
       客户端引用对象 cilent = new TaskService.TaskServiceClient(回调对象, GetNetTcpBinding(true), endadd);
     
       //若需要身份验证
       NetworkCredential credential = cilent.ChannelFactory.Credentials.Windows.ClientCredential;
                        credential.UserName = "用户名";
                        credential.Password = "密码";
                
       cilent.方法()
      

  3.   

    这里错了,变成我的了,应该是客户端引用对象 cilent = new 客户端引用对象(回调对象, GetNetTcpBinding(true), endadd);
      

  4.   

    假设你已经根据某个endpoint生成了代理类,通常情况下,你是这样调用wcf的:
    var client = new MyWCFService.MyServiceClient();
    client.MyMethod();现在因为endpoint地址是动态的,你只需要加这一句:client.Endpoint.Address = new EndpointAddress(new Uri(endpointAddress));