开发者

Calling Client-Side WCF Service With Type List<Of T>

I am designing a multi-file upload service that also shows upload progress for each file.

If I was to design my WCF method as a SOAP contract, I would do something like this:

var request = IService.UploadMethod(List<Upload>开发者_高级运维; request);

But, how do I pass the parameter ""request"" of type "List<Upload>" when I am calling the method from the client (../upload.svc/ uploadpictures/""request"")?

Help appreciated, thanks.


You're using SOAP as you say yourself - which means, you cannot just invoke your service methods from the URL (REST-style), as the second part of your posting would imply (../upload.svc/ uploadpictures/"request").

What you need to do in SOAP is this (assuming you've created your client side proxy using Visual Studio or svcutil):

// create client side proxy, reading URI etc. from config
ServiceClient clientProxy = new ServiceClient();

List<Upload> _list = new List<Upload>();

// add your uploads - don't know what that is - just a filename? The actual
// contents of the file? That's up to you
_list.Add(..........);

var response = clientProxy.UploadMethod(_list);

Marc


Thanks for your answer.

I am using SOAP for part of my API, but I am also using webhttp endpoints for REST - style xhr calls and JSON responses. I have been able to do this elsewhere in the program:

var url = "trade.svc/GetCategoryChildren/" + parentID + "/json";
        var httpStatus;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);

I am trying to do the same thing for my upload service but, as you point out, collecting List<Upload> request = new List<Upload>(); is C#, not JavaScript and I am not quite sure how to pass the parameter in a REST way. Is this possible? Thoughts appreciated?

<service behaviorConfiguration="Default" name="WebClient.UploadService">
        <endpoint address="json" behaviorConfiguration="WebHttpEPBehavior"
                  binding="webHttpBinding" name="MyJSONUploadEP" contract="WebClient.IJSONUploadService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:2534/Web/upload.svc" />
          </baseAddresses>
        </host>
      </service>

IN RESPONSE TO SAHA -

The request should look similar to the xmlhttp request object I have outlined in my previous post. You want to pass your endpoint as the URL argument in xmlhttp.open("GET", url, true). Your parameters should follow the same convention: "service.svc/function/param1/param2/param3".

On the server, you want to design your service's binding as webHttpBinding - take a look at MyJSONUploadEP in my posted config file. Then, in your contract (In my example, WebClient.IJSONUploadService) you want to designate the WebGet attribute with a responeformat of JSON for your requested method. In my contract I have:

[WebGet(UriTemplate = "GetCategoryChildrenJSON/{parentID}/{format}", ResponseFormat = WebMessageFormat.Json)]
Tuple<string, int> GetCategoryChildren(string parentID, string format);

As an aside, I found it useful to have different contracts for different response formats (IJSONUploadService.cs, IPOXUploadService.cs, and ISOAPUploadService.cs which are then inherited as needed). So, for example, your WCF class would look something like:

public class UploadService : IJSONUploadService

If you want to return a number of integers in your JSON response, you should probably design a supporting class with get/set of type integer - something like:

 public class Items
     {
        public int PropA { get; set; }
        public int PropB { get; set; }
        public int PropC { get; set; }
        public int PropD { get; set; }
        public int PropE { get; set; }
        etc... 
     }

Then, after you retrieve your data from the DB, you will need to populate your List (of type Items in this case):

List<Items> categories = new List<Items>();

Next, you will need to serialize your List of type Items for sending back to the client. I am using a class called JSONHelper to do this but there are plenty examples on the Internet. You might start by looking up the
"System.Runtime.Serialization.Json" namespace. Here is what my object for serialization looks like:

string dtSerialized = JSONHelper.Serialize<List<Items>>(categories);
Tuple<int, int> retParam = new Tuple<int, int>(dtSerialized, errCode);
return retParam;

And finally, on the client you will need to handle the JSON response using some sort of JavaScript response handler. There are tons of examples of this online - you might want to try starting with http://json.org/js.html.

HTH, Peter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜