开发者

Consuming a void service operation in a WCF data service

开发者 https://www.devze.com 2023-01-09 10:08 出处:网络
from a Wcf data service client (inherited from System.Data.Services.Client.DataServiceContext) I would like to invoke a service operation defined on a Wcf Data Service that returns void

from a Wcf data service client (inherited from System.Data.Services.Client.DataServiceContext) I would like to invoke a service operation defined on a Wcf Data Service that returns void

[WebGet]
public void Operation()
{
  // logic
}

The only reasonable metho开发者_StackOverflow社区d I found is the Execute, but how could I use it with a void operation?

Thank you


You can use just plain HttpWebRequest to do this. I think it will need to be POST service operation (as GET would assume some response, but since you declare it as void it would have no response). In which case Execute can't be used anyway (as it always issues a GET request). Using plain HttpWebRequest just issue a simple POST to the service operation URL and just check the response status code (should be 204 No Content). Currently WCF Data Services doesn't have native client support for service operations, so you need to write one for yourself.


I have found a workaround for this problem. This website solved me quite a few problems before, so I thought it would be nice to share back.

The quick answer to your question is:

 string empty = context.Execute<string>(new Uri("Operation", UriKind.Relative)).FirstOrDefault();

The "empty" string should be null or empty upon response. It "works around" the HttpWebRequest method mentioned on the post above.

Further more, it is also possible to get primitive types back using this technique. Lets say I have this Method:

[WebGet]
public bool Authenticate(string Username, string Password)
{...do stuff here...}

When you try the normal execution it fails (Vitek Karas explains it well in his reponse above):

    var query = context.CreateQuery<bool>("Authenticate").AddQueryOption("Username", "'itye'").AddQueryOption("Password","'123456'");
DataServiceCollection<bool> list = new DataServiceCollection<bool>();
list.Load(query);

But the following will do the trick:

var query = context.CreateQuery<bool>("Authenticate").AddQueryOption("Username", "'itye'").AddQueryOption("Password","'123456'");

bool authenticated = context.Execute<bool>(new Uri(query.RequestUri.ToString().Replace("Authenticate()", "Authenticate"))).FirstOrDefault();

Please note the Replace("Authenticate()", "Authenticate"), which omits () from the query string (otherwise it will cause error).

Hope it helps. - Itye


Thanks Itye I was looking for similar solution. Did using HttpWebRequest way first. But your two lines of code helped me doing the same task. Very Happy. Thanks Once Again.. var query = context.CreateQuery("Authenticate").AddQueryOption("Username", "'itye'").AddQueryOption("Password","'123456'");

bool authenticated = context.Execute(new Uri(query.RequestUri.ToString().Replace("Authenticate()", "Authenticate"))).FirstOrDefault();

0

精彩评论

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