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

问题描述 投票:1回答:1

我想在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)
c# web-services wcf wsdl
1个回答
1
投票

这是一个例子:

// usings:
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.Configuration;

void ConsumeWebService()
{
    var myBinding = new CustomBinding("xxxxxAPIPortBinding");

    // Manually define Endpoint:
    //var myEndpoint = new EndpointAddress("http://localhost/myservice");

    // Or get from configuration file:
    ClientSection clientSection = (ClientSection)ConfigurationManager.GetSection("system.serviceModel/client");
    ChannelEndpointElement myEndpoint = clientSection.Endpoints[0];

    // Define a communication channel, based on the interface, bindig e endpoint
    var myChannelFactory = new ChannelFactory<xxxxxAPI.PlunetAPI>(myBinding, myEndpoint.Address.AbsoluteUri);
    xxxxxAPI.PlunetAPIclient client = null;

    try
    {
        // Create the channel to consume the service
        client = myChannelFactory.CreateChannel();
        client.MyMethod();  //Calls some method
        ((ICommunicationObject)client).Close();
    }
    catch
    {
        if (client != null)
        {
            ((ICommunicationObject)client).Abort();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.