我想在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)
这是一个示例:
// 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();
}
}
}