views:

45

answers:

2

i have a WCF Method that receives array of structs. the struct contains two strings "Key" and "Value":

public struct mydata
{
    public String key;
    public String value;
}

[ServiceContract]
public interface IBasicService
{

    [OperationContract]
    [WebGet(UriTemplate = "ReceiveStructsOfData?myDataArray={???????? WHAT DO I WRITE HERE?????}")] 
    void ReceiveStructsOfData(mydata[] myDataArray);

}

i want the method to support HTTP "GET". i already know how to config a WCF to support "GET" ( endpoint and WebGETAttribute). What do i write in the UriTemplate of WebGet ( see above example) ??

how will the Client code look like using Framework 2.0 (HttpWebRequest) ?

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/BasicWCF/BasicService.svc/ReceiveStructsOfData?myDataArray={???????? WHAT DO I WRITE HERE?????}");
        myHttpWebRequest.Method = "GET";
        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

thank you...

A: 

The thing you provide between the curly braces is the name of the parameter, which will be substituted by a value. So the service method would look like:

[OperationContract]  
[WebGet(UriTemplate = "ReceiveStructsOfData?myDataArray={myDataArray}")]  
void ReceiveStructsOfData(mydata[] myDataArray)
{
}

Notice the string between the braces is the same as the parameter name - that's how the mapping of requests to methods is done.

When calling the service, you should take care to serialize the array to a string. After all, you are passing parameters in the URL, which is itself a string. Normally I wouldn't pass such a param to a GET method. If it was a PUT method and you were passing params in the request (not the URL), then you could serialize them as JSON or XML, depending on the service.

Slavo
why wouldn't you pass such a param to a "GET" method ?
Rodniko
Normally I would implement a RESTful service, where a GET operation only returns a resource or a collection of resources. If we assume that's the role of the GET operation, then the only thing that such a parameter would be used for is to filter the result. And in that case I would use a string filter expression of some kind, not an array of structs. But that's the way I would go, I'm not saying all the rest is wrong.
Slavo
A: 

Thanks Slavo,

i serialized an example struct array like the above (with just two structs).

MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, myDataArray);
string str = System.Convert.ToBase64String(memoryStream.ToArray());

the result was that i got a long string that looks like this: ""AAEAAAD/////AQAAAAAAAAAMAgAAAERXQ0ZUZXN0Q2xpZW50LCBWZXJzaW9uPTEuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbAcBAAAAAAEAAAACAAAABCZXQ0ZUZXN0Q2xpZW50LlNlcnZpY2VSZWZlcmVuY2UxLm15ZGF0YQIAAAAMBAAAAElTeXN0ZW0sIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5Bf3///8mV0NGVGVzdENsaWVudC5TZXJ2aWNlUmVmZXJlbmNlMS5teWRhdGEDAAAACGtleUZpZWxkCnZhbHVlRmllbGQPUHJvcGVydHlDaGFuZ2VkAQEEMVN5c3RlbS5Db21wb25lbnRNb2RlbC5Qcm9wZXJ0eUNoYW5nZWRFdmVudEhhbmRsZXIEAAAAAgAAAAYFAAAAClVzZXIgQWdlbnQGBgAAAAlOb2tpYSBOODYKAfn////9////BggAAAAHVmVyc2lvbgYJAAAABDEuNDQKCw=="

It is too long , considering that my client has to add it into the "GET" URL and considering this is only an array of only two structs. is here a different shorter way to send a struct Array in a "GET" http request?

Rodniko
You should have probably added this to the question, or as a comment, not a new answer. Anyway, you could use a JSON serializer or an XmlSerializer, but that could also get long, depending on the struct. You cannot depend on knowing how many structs will be passed. What if someone else calls the service? A whole array in a URL is not a good idea, I think.
Slavo