简体   繁体   English

从c#client调用ac#wcf webservice

[英]calling a c# wcf webservice from c# client

i have ac# wcf web service and ac# client which makes calls to the web service which is working fine and have a scenario like below which i am unable to figure it out how to do it. 我有一个#wcf web服务和ac#客户端,它调用Web服务工作正常,并有一个像下面的情况,我无法弄清楚如何做到这一点。

Here is the case, i have 2 methods in c# client as the 1st method will make more time and client doesn't know what going on as response from the wcf web service will take long time, so we implemented 2nd method which gives the status of the 1st method call(saying that the first method has a total of 10 tasks and is currently performing 1 or 2 or 3 etc). 在这种情况下,我在c#client中有2个方法,因为第一种方法会占用更多时间,客户端不知道wcf web服务的响应会花多长时间,所以我们实现了第二种方法给出了状态第一种方法调用(说第一种方法总共有10个任务,当前正在执行1或2或3等)。 now i don't have any idea how the call goes to the 2nd method as the first method is not yet completed and as both the methods have to be in the same client. 现在我不知道调用如何进入第二种方法,因为第一种方法还没有完成,因为这两种方法都必须在同一个客户端。 Can any one please help with this. 任何人都可以帮忙解决这个问题。

The very best way of solving this kind of issue is to split tasks across multiple different WCF methods, chaining the calls. 解决此类问题的最佳方法是跨多个不同的WCF方法拆分任务,将调用链接起来。 Example: 例:

instead of doing this (just a proof of concept): 而不是这样做(只是一个概念证明):

serviceClient = new MyWCFClient();
serviceClient.TaskThatTakesForeverCompleted += (s,e) => { /*I'm done !*/ };
serviceClient.TaskThatTakesForeverAsync();

you'll end up doing this: 你最终会这样做:

serviceClient = new MyWCFClient();
serviceClient.FirstTaskCompleted += (s,e) => { /*first task done!*/ serviceClient.SecondTaskAsync(); };
serviceClient.SecondTaskCompleted += (s,e) => { /*and so on... */ };
...
serviceClient.FirstTaskAsync();

while server-side you'd implement it like this: 虽然服务器端你实现它像这样:

[OperationContract]
public <return type> FirstTask() { /* code */}
[OperationContract]
public <return type> SecondTask() { /* more code */ }

Of course, this might not apply to your case, but very long tasks can be split up in this fashion. 当然,这可能不适用于您的情况,但很长的任务可以这种方式分开。

WARNING : This kind of implementation is most definitely not best-practice since you'll find yourself with a bunch of methods which have to be invoked in a fixed order (FirstTask must be the first, then goes SecondTask, and so on) but in the end it would handle the issue of knowing what the service is doing. 警告 :这种实现绝对不是最佳实践,因为你会发现自己有一堆必须以固定顺序调用的方法(FirstTask必须是第一个,然后是SecondTask,依此类推)但是最终它会处理知道服务正在做什么的问题。

It really is harder to explain than to understand, all it takes is a little fiddling around it's quite a straightforward logic. 解释起来真的比理解起来要困难得多,所需要的只是摆弄它是一个非常简单的逻辑。

NOTE : If you don't have the Async calls, enable them this way: 注意 :如果您没有异步调用,请以这种方式启用它们:

  • Right click the service reference 右键单击服务引用
  • "Configure service reference" “配置服务参考”
  • Check "generate asynchronous operations", leave "generate message contracts" unchecked and everything else as is 检查“生成异步操作”,不选中“生成消息合同”,其他所有内容保持原样
  • Now you'll have the async calls at your disposal 现在,您可以随时使用异步调用

If I have understand correctly... you need to implement asynchronous invocation . 如果我理解正确...你需要实现异步调用 By default in WCF all calls are synchronous. 默认情况下,WCF中的所有调用都是同步的。

What you have now is a synchronous invocation.: 你现在拥有的是同步调用:

  1. Method1 (Client) invokes Method2 (Server) to have tasks done. Method1(Client)调用Method2(Server)完成任务。
  2. Method1 waits a long time until Method2 finishes Method1等待很长时间,直到Method2完成
  3. Method1 receives a true (or whatever) from Method 2 Method1从方法2接收true(或其他)
  4. Method 1can continue doing things. 方法1可以继续做事。

What you need is: 你需要的是:

  1. Method1 (Client) calls Method2 (Server) Method1(Client)调用Method2(Server)
  2. Method2 方法2

    2.1 Validate that request is correctly formed 2.1验证请求是否正确形成

    2.2 Creates a thread with working process 2.2使用工作流程创建一个线程

    2.3 Returns true (that means ok, I have queued your request) 2.3返回true(表示确定,我已将您的请求排队)

    2.4 Method1 continue doing its stuff. 2.4 Method1继续做它的东西。

Then you have two options: 那你有两个选择:

3A. 3A。 When Method2 finishes it calls another method in Client to report that it has finished (Say Method 3 in Client) 当Method2完成时,它调用Client中的另一个方法来报告它已经完成(在客户端中说方法3)

3B. 3B。 Method1 query another method in Server (Say Method4 in Server) to see progress of task every x seconds and show it on screen or do whatever it wants until it is finished. Method1查询服务器中的另一个方法(服务器中的Say方法4),每隔x秒查看任务进度,并在屏幕上显示或执行任何操作,直到完成为止。

You can check this MSDN link about how to implement asynchronous methods in WCF. 您可以查看此MSDN链接,了解如何在WCF中实现异步方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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