想要在没有App.config的C#客户端中使用WSDL Web服务(但是通过代码)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了想要在没有App.config的C#客户端中使用WSDL Web服务(但是通过代码)相关的知识,希望对你有一定的参考价值。
我想在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();
}
}
}
以上是关于想要在没有App.config的C#客户端中使用WSDL Web服务(但是通过代码)的主要内容,如果未能解决你的问题,请参考以下文章
从.Net到vb6的Web服务的DLL - 如何设置app.config
WCF 何时使用 app.config 或 web.config?
C# Windows 服务 - 从 ini 或 App.config 文件中读取