想要在没有App.config的C#客户端中使用WSDL Web服务(但是通过代码)

时间:2018-03-01 09:42:08

标签: c# web-services wcf wsdl

我想在C#类库项目中使用WSDL Web服务而不更改app.config文件。我有限制使用某些第三方平台更改app.config,我只需要上传DLL。 添加服务引用后,app.config看起来像是:

<configuration> 
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="xxxxxAPIPortBinding">
                    <textMessageEncoding messageVersion="Soap12" />
                    <httpsTransport />
                </binding>               
            </customBinding>
        </bindings>
        <client>
            <endpoint address="https://xxxxxxx.com:443/xxxxxAPI"
                binding="customBinding" bindingConfiguration="xxxxxAPIPortBinding"
                contract="xxxxxAPI.xxxxxAPI" name="xxxxxAPIPort" />            
        </client>
    </system.serviceModel>
</configuration> 

如何在C#类中使用上述配置的Web服务?

我尝试了以下方法:

HttpsTransportBindingElement httpTransport = new HttpsTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };

TextMessageEncodingBindingElement TextMessage = new TextMessageEncodingBindingElement() { MessageVersion = MessageVersion.Soap12 };

CustomBinding customBinding = new CustomBinding(TextMessage, httpTransport);

ChannelFactory<IMyService>  channelFactory = new ChannelFactory<IMyService>(customBinding);
EndpointAddress ep = new EndpointAddress("https://xxxxxxxxx.com:443/AnyService?wsdl");

IMyService serviceObj = channelFactory.CreateChannel(ep);

string uuid = serviceObj.login(USERNAME, PASSWORD);

但得到了错误

System.ServiceModel.FaultException: Cannot find dispatch method for {http://tempuri.org/}login

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)

Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

1 个答案:

答案 0 :(得分:1)

以下是一个例子:

SSLContext::init