我必须执行以下代码:
BasicHttpBinding binding = new BasicHttpBinding ();
Uri baseAddress = new Uri ("URL.svc");
EndpointAddress endpointAddress = new EndpointAddress (baseAddress);
var myChannelFactory = new ChannelFactory<IMyInterface> (binding, endpointAddress);
IMyInterface client = null;
try
{
client = myChannelFactory.CreateChannel ();
var a = client.WsFunction ("XXXXXX");
((ICommunicationObject)client).Close ();
}
catch
{
if (client != null)
{
((ICommunicationObject)client).Abort ();
}
}
其中“IMyInterface”是我的WS实现的接口(interface)。例如:
[ServiceContract]
public interface IMyInterface
{
[OperationContract]
Result WsFunction1 (string param);
[OperationContract]
Result WsFunction2 (string param);
[OperationContract]
Result WsFunction3 (string param);
}
它返回如下内容:
[DataContract]
public class Result
{
string a = "";
string b = "";
[DataMember]
public string A
{
get { return a; }
set { a = value; }
}
[DataMember]
public string B
{
get { return b; }
set { b = value; }
}
}
当我运行此代码时,可以到达WS,但是无法填写结果。
我究竟做错了什么?
提前致谢!
最佳答案
通过BasicHttpBinding
访问服务的最简单方法是从SilverSilver实用程序应用程序SlSvcUtil.exe生成客户端代码。
SLsvcUtil.exe /directory:C:\users\me\Desktop http://URL.svc
那应该在它生成的文件内部创建一个MyInterfaceClient类。
然后,您可以在代码中执行以下操作:
var binding = new BasicHttpBinding() {
Name = "BindingName",
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
var endpoint = new EndpointAddress("URL.svc");
MyInterfaceClient client = new MyInterfaceClient(binding, endpoint);
client.WSFunctionCompleted += (object sender, WSFunctionCompletedEventArgs e) => {
//access e.Result here
};
client.WSFunctionAsync("XXXXXX");
你的旅费可能会改变。让我知道这个是否奏效。
关于c# - 如何以编程方式创建BasicHttpBinding?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19774540/