我必须遵循以下代码:
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实现的接口...例如:
[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,但我永远无法填写结果。
我做错了什么?
提前致谢!
答案 0 :(得分:9)
通过BasicHttpBinding
访问服务的最简单方法是从SlSvcUtil.exe生成客户端代码,这是一个silverlight实用程序。
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");
您的里程可能会有所不同。让我知道这个是否奏效。
答案 1 :(得分:1)
select json_build_object('identifier', identifier, 'name', name)
from country
如果你想在你的本地运行它,你应该这个代码。
var binding = new BasicHttpBinding();
binding.ProxyAddress = new Uri(string.Format("http://{0}:{1}", proxyAddress, proxyPort));
binding.UseDefaultWebProxy = false;
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
var endpoint = new EndpointAddress("serviceadress");
var authenticationClient = new WOKMWSAuthenticateClient(binding, endpoint);
authenticationClient.ClientCredentials.UserName.UserName = username;
authenticationClient.ClientCredentials.UserName.Password = password;
答案 2 :(得分:0)
调用WCF的简单方法非常简单:
BasicHttpBinding myBinding = new BasicHttpBinding();
EndpointAddress myEndpoint = new EndpointAddress("http://localhost:3283/Service1.svc");
myBinding.ReaderQuotas.MaxArrayLength = int.MaxValue;
myBinding.MaxBufferSize = int.MaxValue;
myBinding.MaxReceivedMessageSize = int.MaxValue;
ChannelFactory<ITestAPI> myChannelFactory = new ChannelFactory<ITestAPI>(myBinding, myEndpoint);
ITestAPI instance = myChannelFactory.CreateChannel();
Test data = new Test();
data.helloName = name;
data= instance.GetMyName(data);
myChannelFactory.Close();