简体   繁体   English

如何同步调用WCF服务

[英]How to call a WCF service Synchronously

I have a WCF service and I am creating the client using the "Add service reference" from VS 2010. 我有一个WCF服务,我使用VS 2010中的“添加服务引用”创建客户端。

The issue is that service is being invoked asynchronously though "Generate Asynchronous Operations" options unchecked. 问题是,虽然未选中“生成异步操作”选项,但异步调用服务。 在此输入图像描述

So how can I call the service Synchronously ? 那么如何同步调用服务呢? Where is this behavior defined (on the client or server) ? 这个行为在哪里定义(在客户端或服务器上)? I am kind of new to WCF.Kindly enlighten 我是WCF的新手。亲切地指教

Client is a console application. 客户端是控制台应用程序。

I have the "Generate asynchronous operations" unchecked. 我没有选中“生成异步操作”。 Even then the proxy contains the following lines which indicate that the method is called Asynchronously.Dont know why :) 即使这样代理包含以下行,表明该方法被称为异步。不知道为什么:)

 [System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="urn:COBService")]
    [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(MemberType))]
    void ABC(TestProject.ServiceReference1.ProcessCOBRecord request);

    [System.ServiceModel.OperationContractAttribute(IsOneWay=true, AsyncPattern=true, Action="urn:COBService")]
    System.IAsyncResult BeginABC(TestProject.ServiceReference1.ProcessCOBRecord request, System.**AsyncCallback** callback, object asyncState);

    void EndABC(System.IAsyncResult result);

Update 更新

It turns out the WCF service configuration was causing this asynchronous behavior, specifically the IsOneWay property of the OperationContract attribute . 事实证明,WCF服务配置导致了这种异步行为,特别是OperationContract属性IsOneWay属性 This is not technically asynchronous, but it "usually gives the appearance of asynchronous call" . 这在技术上并不是异步的,但它“通常会给出异步调用的外观”


You don't have to do anything special, just invoke the normal method on the client proxy -- that's the synchronous method. 您不必执行任何特殊操作,只需在客户端代理上调用常规方法 - 这就是同步方法。 So if you have a WCF method named DoSomething , then you'd just call: 因此,如果您有一个名为DoSomething的WCF方法,那么您只需调用:

var client = new MyService.MyServiceClient();
client.DoSomething();

It's client.DoSomethingAsync that is the asynchronous method. 它是client.DoSomethingAsync ,它是异步方法。

This distinction relates to the client behavior, whether or not your application blocks the thread while waiting for the WCF service to respond. 这种区别与客户端行为有关,无论您的应用程序是否在等待WCF服务响应时阻塞该线程。

After you're done adding the service reference you should get synchronous methods for each exposed service operation. 添加完服务引用后,您应该为每个公开的服务操作获取同步方法。

Sychronous methods are named the same as the service operation, eg GetCustomers . 同步方法的命名与服务操作相同,例如GetCustomers Async methods on the other hand are generated in two ways: GetCustomersAsync , BeginGetCustomers / EndGetCustomers . 另一方面,异步方法以两种方式生成: GetCustomersAsyncBeginGetCustomers / EndGetCustomers

If you want to get customers synchronously, you need to call GetCustomers . 如果要同步获取客户,则需要调用GetCustomers In that case, GetCustomers will block until the service operation is completed and then the code moves on the next line. 在这种情况下, GetCustomers将阻塞,直到服务操作完成,然后代码在下一行移动。

If the Generate asynchronous operations option is unchecked then the service will be called synchronously 如果未选中Generate asynchronous operations选项,则将同步调用该服务

From MSDN 来自MSDN

Generate asynchronous operations 生成异步操作
Determines whether WCF service methods will be called synchronously (the default) or asynchronously. 确定是同步调用(默认)还是异步调用WCF服务方法。

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

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