C# 使用 WCF 回调需要采取哪些步骤?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1044174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What steps do I need to take to use WCF Callbacks?
提问by kevin bailey
I am trying to learn WCF. I have a simple client and server application setup and upon pressing a button on the client, it gets an updated value from the server.
我正在尝试学习 WCF。我有一个简单的客户端和服务器应用程序设置,按下客户端上的按钮后,它会从服务器获取更新的值。
My next step is I am trying to do a callback from the server to the client to update its value. I have poured through many examples, and they just seem too big and confusing. Is there anyone that can give my just the simplest example of its implementation in C#?
我的下一步是尝试从服务器到客户端进行回调以更新其值。我已经列举了很多例子,但它们看起来太大而令人困惑。有没有人可以给我在 C# 中实现它的最简单的例子?
I keep looking through examples online and I just do not understand what it takes? Of course I could copy the example line by line but that does me no good because I still don't what to implement if I wanted to do this in my own code.
我一直在网上浏览示例,但我不明白这需要什么?当然,我可以逐行复制示例,但这对我没有好处,因为如果我想在自己的代码中执行此操作,我仍然不知道要实现什么。
Could someone please help me with a very simple example on what steps I would need to take and what I would need to do in the server code and then in the client code to make this happen?
有人可以用一个非常简单的例子来帮助我说明我需要采取哪些步骤以及我需要在服务器代码和客户端代码中做什么来实现这一点吗?
Thank you
谢谢
采纳答案by Ray Vernagus
Here is about the simplest complete example that I can come up with:
这是我能想到的最简单的完整示例:
public interface IMyContractCallback
{
[OperationContract]
void OnCallback();
}
[ServiceContract(CallbackContract = typeof(IMyContractCallback))]
public interface IMyContract
{
[OperationContract]
void DoSomething();
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class MyService : IMyContract
{
public void DoSomething()
{
Console.WriteLine("Hi from server!");
var callback = OperationContext.Current.GetCallbackChannel<IMyContractCallback>();
callback.OnCallback();
}
}
public class MyContractClient : DuplexClientBase<IMyContract>
{
public MyContractClient(object callbackInstance, Binding binding, EndpointAddress remoteAddress)
: base(callbackInstance, binding, remoteAddress) { }
}
public class MyCallbackClient : IMyContractCallback
{
public void OnCallback()
{
Console.WriteLine("Hi from client!");
}
}
class Program
{
static void Main(string[] args)
{
var uri = new Uri("net.tcp://localhost");
var binding = new NetTcpBinding();
var host = new ServiceHost(typeof(MyService), uri);
host.AddServiceEndpoint(typeof(IMyContract), binding, "");
host.Open();
var callback = new MyCallbackClient();
var client = new MyContractClient(callback, binding, new EndpointAddress(uri));
var proxy = client.ChannelFactory.CreateChannel();
proxy.DoSomething();
// Printed in console:
// Hi from server!
// Hi from client!
client.Close();
host.Close();
}
}
A few namespaces will need to be included in order to run the example:
为了运行示例,需要包含一些命名空间:
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
回答by Michael Meadows
If I'm reading your question right, you want to have a two-way conversation between the client and the server (the server can communicate back to the client). The WSDualHttpBindinggives you this functionality.
如果我正确地阅读了您的问题,您希望在客户端和服务器之间进行双向对话(服务器可以与客户端进行通信)。该WSDualHttpBinding为您提供了此功能。
The unfortunate reality with WCF is that there is no such thing as a simple example. It requires you to define contracts, configure the services, and use a host, and create client code. Take a look at this articlefor a somewhat simple example.
WCF 的不幸现实是没有简单示例这样的东西。它要求您定义合同、配置服务、使用主机并创建客户端代码。看一下这篇文章的一个有点简单的例子。
回答by Tad Donaghe
Grab a copy of "Programming WCF Services, 2nd Edition" by Juval Lowy. There are large sections of the book devoted to Callback operations. In Chapter 5, start on page 214. In the chapter on Concurrency Management (ch. 8) there's even more information.
获取 Juval Lowy 撰写的“Programming WCF Services, 2nd Edition”的副本。书中有很大一部分专门介绍回调操作。在第 5 章,从第 214 页开始。在并发管理(第 8 章)一章中有更多信息。
"Programming WCF Services" is more or less the WCF "bible."
“WCF 服务编程”或多或少是 WCF 的“圣经”。
回答by в?a???? в?н???
I know, old question... I came across this question from a google search earlier today and the answer provided by Ray Vernagus is the easiest to understand example of WCF that I have read to date. So much so that I was able to rewrite it in VB.NET without using any online converters. I thought I'd add the VB.NET variant of the example that Ray Vernagus provided. Just create a new VB.NET Windows Console application, add a reference to System.ServiceModel
, and copy/paste the entire code below into the default Module1
class file.
我知道,老问题……我今天早些时候在谷歌搜索中遇到了这个问题,Ray Vernagus 提供的答案是迄今为止我读过的最容易理解的 WCF 示例。以至于我能够在不使用任何在线转换器的情况下在 VB.NET 中重写它。我想我会添加 Ray Vernagus 提供的示例的 VB.NET 变体。只需创建一个新的 VB.NET Windows 控制台应用程序,添加对 的引用System.ServiceModel
,然后将下面的整个代码复制/粘贴到默认Module1
类文件中。
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Public Interface IMyContractCallback
<OperationContract()> _
Sub OnCallBack()
End Interface
<ServiceContract(CallBackContract:=GetType(IMyContractCallback))> _
Public Interface IMyContract
<OperationContract()> _
Sub DoSomething()
End Interface
<ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Reentrant)> _
Public Class Myservice
Implements IMyContract
Public Sub DoSomething() Implements IMyContract.DoSomething
Console.WriteLine("Hi from server!")
Dim callback As IMyContractCallback = OperationContext.Current.GetCallbackChannel(Of IMyContractCallback)()
callback.OnCallBack()
End Sub
End Class
Public Class MyContractClient
Inherits DuplexClientBase(Of IMyContract)
Public Sub New(ByVal callbackinstance As Object, ByVal binding As Binding, ByVal remoteAddress As EndpointAddress)
MyBase.New(callbackinstance, binding, remoteAddress)
End Sub
End Class
Public Class MyCallbackClient
Implements IMyContractCallback
Public Sub OnCallBack() Implements IMyContractCallback.OnCallBack
Console.WriteLine("Hi from client!")
End Sub
End Class
Module Module1
Sub Main()
Dim uri As New Uri("net.tcp://localhost")
Dim binding As New NetTcpBinding()
Dim host As New ServiceHost(GetType(Myservice), uri)
host.AddServiceEndpoint(GetType(IMyContract), binding, "")
host.Open()
Dim callback As New MyCallbackClient()
Dim client As New MyContractClient(callback, binding, New EndpointAddress(uri))
Dim proxy As IMyContract = client.ChannelFactory.CreateChannel()
proxy.DoSomething()
' Printed in console:
' Hi from server!
' Hi from client!
Console.ReadLine()
client.Close()
host.Close()
End Sub
End Module