C# WCF客户端无法将app.config转换为代码
我现在尝试了至少两个小时,到处搜索,但不知道如何用代码编写:C# WCF客户端无法将app.config转换为代码,c#,.net,wcf,C#,.net,Wcf,我现在尝试了至少两个小时,到处搜索,但不知道如何用代码编写: <system.serviceModel> <bindings> <basicHttpBinding> <binding name="TCTBinding"> <security mode="Transport"> <transport clientCredentialType="Basic" proxyCredentialTyp
<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>
但它不起作用:
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)
但是这是可行的(从app.config获取绑定):
我错过了什么?不要像那样更新你的服务,试试ChannelFactory:
ChannelFactory<ServiceReference1.TCTClient> chan =
new ChannelFactory<ServiceReference1.ITCTClient>(binding, new EndpointAddress(url));
chan.Open();
ServiceRefernece1.TCTClient client = chan.CreateChannel();
client.DotheThing(....
ChannelFactory chan=
新的ChannelFactory(绑定,新的端点地址(url));
chan.Open();
ServiceRefernece1.tctcClient=chan.CreateChannel();
客户。做某事(。。。。
谢谢你的建议,但这能解决我的问题吗?@morcibacsi我不知道,是吗?这就是我在代码中创建服务而不是在配置中创建服务的方式。好吧,我有时间尝试了,不幸的是,这不能解决我的问题。
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;
}
ChannelFactory<ServiceReference1.TCTClient> chan =
new ChannelFactory<ServiceReference1.ITCTClient>(binding, new EndpointAddress(url));
chan.Open();
ServiceRefernece1.TCTClient client = chan.CreateChannel();
client.DotheThing(....