0

I've been using the WCF Web Api recently and i've been using the WCF Web API Test Client that is built in to it to test my created webservices.

I am wanting to create a proxy in code built off of the interface rather than run svcutil.exe to create a proxy.

My webservice is working fine, however when I use fiddler to examine the message that is sent, it is putting in a namespace into the xml message.

Below is the code I use to send the request.

        RegisterRequest registerRequest = new RegisterRequest
                                              {
                                                  Email = "[email protected]",
                                                  Firstname = "firstname",
                                                  Lastname = "lastname",
                                                  Password = "password"
                                              };


        var factory = new ChannelFactory<IAccountApi>(new WebHttpBinding(), "http://localhost/WebServices/api/account");
        factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
        var proxy = factory.CreateChannel();
        proxy.Register(registerRequest); 

This request below is generated via the client, and it fails, returning a 500 internal server error

<RegisterRequest xmlns="http://schemas.datacontract.org/2004/07/ServiceModel.Accounts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Email>[email protected]</Email><Firstname>firstname</Firstname><Lastname>lastname</Lastname><Password>password</Password></RegisterRequest>

Using the same headers when I send using the api test client the following message passes

<RegisterRequest><Email>[email protected]</Email><Firstname>firstname</Firstname><Lastname>lastname</Lastname><Password>password</Password></RegisterRequest>

The only difference being the namespace has been removed.

Some final points,

1) If I were able to remove this namespace the request would work

2) I am not sure if ChannelFactory can be used in conjunction with WCF Web Api. The reason being http://wcf.codeplex.com/releases/view/73423 states "[ServiceContract] is no longer required on the Web API class definition", yet Channel Factory requires it.

3) All the examples so far from the WCF Web API look like the following

HttpClient client = new HttpClient();

Contact contact = new Contact() { Name = name };
var response = client.Post("http://localhost:9000/api/contacts/",
    new ObjectContent<Contact>(
        contact, JsonMediaTypeFormatter.DefaultMediaType));

Should I be using HttpClient for my requests instead of channel factory?

Regards, Andrew

2 Answers 2

1

It appears that the IAccountApi, which you do not show, is defining a namespace for the service contract. If you really want an empty namespace (not best practice) try something like this:

 [ServiceContract(Namespace="")]
 public interface IAccountApi
 { ... }

If the namespace is not defined for IAccountApi, check the [DataContract] of RegisterRequest.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Richard, even with the namespace taken out it still puts xmlns:i="w3.org/2001/XMLSchema-instance into the request which causes issues.
0

I ended up using HttpClient class, it allows GET, POST, PUT and DELETE which was fine for WCF Web API (now called http://www.asp.net/web-api)

http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx

as far as building a rest proxy using codegen or being dynamic see this article ReST Proxy Object Generator

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.