简体   繁体   中英

C# WPF Service custom request format

I have a WCF Serivce that reads requests with XML payloads and responds to these requests. For example, a simple login request would look like:

<?xml version="1.0" encoding="utf-8"?>
<LoginRequest>
    <username>test</username>
    <password>foo</password>
</LoginRequest>

Now I know I could simply accept XElement in my service method but is there any way I can tell the underlying system how to read the above XML and translate it into a function call in such format:

public LoginResponse Login (string username, string password); 

Is such a thing possible?

I don't believe that your specified XML will actully work formatted as it is. I only say that because unless you change the SOAP message comming in, it will recognize the XML inside as a single string value (as it seems like you have now) and not multiple values.

If you have a .NET client, then you would want to utilize the same classes, decorated with the DataContract attribute, on the client side (or proxy objects) that already know how to serialize and deserialze to SOAP formats. Since you do not specify what the client is that is sending you the XML payloads, there is a good possiblity that the XML is already wrapped in a SOAP envelope and would require some overhaul on both sides to be "more correct".

Additionally, because of what you got going here, your actual SOAP requests would have to change significantly to pass those as arguments instead of just an XML value.

So, I guess the bottom line answer is no. You would not be able to change the WCF service alone to do this.

Yes it is possible.

a) After changing the xml to

<Login>
     <username>test</username>
     <password>foo</password>
</Login>

b) replacing [ServiceContract] with [ServiceContract(Namespace = "")]

c) and declaring method as

[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Xml, 
           ResponseFormat = WebMessageFormat.Xml, 
           BodyStyle = WebMessageBodyStyle.Wrapped)]
public string Login(string username, string password){}

It workes....

Here is the code I used to test

public void TestWCFService()
{
    //Start Server
    Task.Factory.StartNew(
        (_) =>
        {
            Uri baseAddress = new Uri("http://localhost:8080/Test");
            WebServiceHost host = new WebServiceHost(typeof(TestService), 
                                                     baseAddress);
            host.Open();
        }, null, TaskCreationOptions.LongRunning).Wait();


    //Client
    string xml = @"<Login>
                      <username>test</username>
                      <password>foo</password>
                  </Login>";

    var wc = new WebClient();
    wc.Headers.Add("Content-Type", "application/xml; charset=utf-8");
    var result = wc.UploadString("http://localhost:8080/Test/Login", xml);
}

[ServiceContract(Namespace = "")]
public class TestService
{
    [OperationContract]
    [WebInvoke(RequestFormat = WebMessageFormat.Xml, 
               ResponseFormat = WebMessageFormat.Xml, 
               BodyStyle = WebMessageBodyStyle.Wrapped)]
    public string Login(string username, string password)
    {
        return username + " " + password;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM