简体   繁体   English

WCF客户端无法将app.config转换为代码

[英]WCF client cannot convert app.config to code

I'm trying at least two hours now, searching everywhere, but can't figure out how to write this in code: 我现在尝试至少两个小时,到处搜索,但是无法弄清楚如何用代码编写:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="TCTBinding">
      <security mode="Transport">
        <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="xxx" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="https://omitted"
    binding="basicHttpBinding" bindingConfiguration="TCTBinding"
    contract="ServiceReference1.DoTheThing" name="HTTPS_Port" />
</client>

I've come up with this: 我想出了这个:

            var binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.Transport;
            binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
            binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
            binding.Security.Transport.Realm = "xxx";

            string url = "https://omitted";

            using (ServiceReference1.TCTClient cl = new ServiceReference1.TCTClient(binding, new EndpointAddress(url)))
            {
                cl.ClientCredentials.UserName.UserName = "user";
                cl.ClientCredentials.UserName.Password = "pass";


                var req = new ServiceReference1.MyRequest()
                {
                    DATA_START = "1234",
                    DATA_STOP = "1234"
                };
                var x = cl.DoTheThing(req);
                richTextBox1.Text += "RESPONSE FROM SERVER: " + x.RESPONSE;
            }

But it is not working: 但它不起作用:

Type          : System.ServiceModel.FaultException
Message       : Server Error
Source        : mscorlib
DeclaringType : System.Runtime.Remoting.Proxies.RealProxy
TargetSite    : Void HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage, System.Runtime.Remoting.Messaging.IMessage)

StackTrace:

Server stack trace: 
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

However this works (getting the binding from app.config): 但这可行(从app.config获取绑定):

                using (ServiceReference1.TCTClient cl = new ServiceReference1.TCTClient(HTTPS_Port))
            {
                cl.ClientCredentials.UserName.UserName = "user";
                cl.ClientCredentials.UserName.Password = "pass";


                var req = new ServiceReference1.MyRequest()
                {
                    DATA_START = "1234",
                    DATA_STOP = "1234"
                };
                var x = cl.DoTheThing(req);
                richTextBox1.Text += "RESPONSE FROM SERVER: " + x.RESPONSE;
            }

What am I missing? 我想念什么?

Instead of newing up your service like that, try a ChannelFactory: 与其尝试像这样更新服务,不如尝试一个ChannelFactory:

ChannelFactory<ServiceReference1.TCTClient> chan = 
   new ChannelFactory<ServiceReference1.ITCTClient>(binding, new EndpointAddress(url));
chan.Open();
ServiceRefernece1.TCTClient client = chan.CreateChannel();

client.DotheThing(....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM