12

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!

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
briba
  • 2,857
  • 2
  • 31
  • 59
  • That wouldn't compile, little a is of type string, big A is of type bool. – Wiktor Zychla Nov 04 '13 at 18:38
  • It's because I changed the names... but actually it compiles and WS receives the message... but I can't get the result.. – briba Nov 04 '13 at 18:42
  • 1
    Result namespace is one of probale culprits but I would start from sniffing the traffic with an http debugger. – Wiktor Zychla Nov 04 '13 at 18:44
  • 1
    Using Fiddler I can see that I can reach the WS perfectly! But Result is the guilty... always null... – briba Nov 04 '13 at 18:53
  • 1
    can you show your implementation of IMyInterface? – Millie Smith Nov 04 '13 at 18:54
  • It's in the post =) ...I just changed the names... but actually, I have 3 methods that return a "Result" (DataContract) – briba Nov 04 '13 at 18:56
  • When there is a xml namespace mismatch, the response reaches the client but is not deserialized correctly. Could be just happening to you. – Wiktor Zychla Nov 04 '13 at 18:57
  • But it woulnd't return an exception? Actually I copied the DataContract from WS... =/ ...guess it's correct – briba Nov 04 '13 at 18:58
  • Can you explain why you want to do it "by hand"? Perhaps generating a proxy with SvcUtil (in VS Console) will give you a perfect proxy for that. If you want more information on what is going on, just read that proxy code. – Mare Infinitus Nov 04 '13 at 20:01
  • I want to make a dll and remove the app.config step =) – briba Nov 04 '13 at 20:08

3 Answers3

9

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.

Millie Smith
  • 4,536
  • 2
  • 24
  • 60
3
 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;
aemre
  • 2,351
  • 2
  • 17
  • 20
0

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();
Dilip Oganiya
  • 1,504
  • 12
  • 20