質問

I have to following code:

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 ();
    }
}

Where "IMyInterface" is the interface that my WS implements.. for example:

[ServiceContract]
public interface IMyInterface
{
    [OperationContract]
    Result WsFunction1 (string param);

    [OperationContract]
    Result WsFunction2 (string param);

    [OperationContract]
    Result WsFunction3 (string param);
}

And it returns something like this:

[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; }
    }
}

When I run this code, I can reach the WS, but I can never get the Result filled out.

What am I doing wrong?

Thanks in advance!

役に立ちましたか?

解決

The easiest way to access a service via a BasicHttpBinding is to generate the client code from SlSvcUtil.exe, which is a silverlight utility application.

SLsvcUtil.exe /directory:C:\users\me\Desktop http://URL.svc

That should create a MyInterfaceClient class inside of the file it generates.

Then in your code you can do:

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");

Your mileage may vary. Let me know if this works.

他のヒント

 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;

if you want to run it on your local you should this code.

 ServicePointManager.Expect100Continue = false;

Very easy and simple way to call 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();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top