简体   繁体   中英

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):

                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<ServiceReference1.TCTClient> chan = 
   new ChannelFactory<ServiceReference1.ITCTClient>(binding, new EndpointAddress(url));
chan.Open();
ServiceRefernece1.TCTClient client = chan.CreateChannel();

client.DotheThing(....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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