English 中文(简体)
多功能妇女论坛
原标题:Multi WCF CallBack Interfaces on WPF
  • 时间:2011-11-24 10:28:46
  •  标签:
  • wcf

I have to communicate 2 WPF application. To communicate, i am using a WCF windows service running on local machine.

当有人要求使用某种服务方法时,服务又要回头。 只有一个反馈接口,所有方法都在其中书写。 但是,有2个世界森林论坛建议没有采用同样的反馈方法。 因此,我被迫采用未使用的方法。

因此,我试图发现,我是否能够在服务上建立2个不同的、独立的接战接口,但可以肯定。 是否有办法这样做?

<>>>>>

我的样本守则:

IDeviceCallBack

public interface ITestCallBack1
{
    [OperationContract(IsOneWay = true)]
    void Test1();
}

public interface ITestCallBack2
{
    [OperationContract(IsOneWay = true)]
    void Test2();
}

public interface IDeviceCallback : ITestCallBack1, ITestCallBack2
{ }

IDevice

[ServiceContract(CallbackContract = typeof(ITestCallBack1))]
public interface ITestContract1
{ }

[ServiceContract(CallbackContract = typeof(ITestCallBack2))]
public interface ITestContract2
{ }


[ServiceContract(CallbackContract = typeof(IDeviceCallback))]
public interface IDevice : ITestContract1, ITestContract2
{
    [OperationContract]
    bool Subscribe();

    [OperationContract]
    bool Unsubscribe();
}

What I want: WPF1

[CallbackBehaviorAttribute(ConcurrencyMode = ConcurrencyMode.Multiple)]
public partial class MainWindow : Window, ITestCallBack1, IDisposable//,IDeviceCallBack
{
    private InstanceContext context;
    private DeviceClient deviceClient;

    public MainWindow()
    {
        InitializeComponent();
        context = new InstanceContext(this);
        deviceClient = new DeviceServiceReference.DeviceClient(context);
    }

    public void Dispose()
    {
        deviceClient.Close();
    }

    public void Test1()
    {
        throw new NotImplementedException();
    }

    // Not Wanted
    //public void Test2()
    //{
    //    throw new NotImplementedException();
    //}
}

WPF2

[CallbackBehaviorAttribute(ConcurrencyMode = ConcurrencyMode.Multiple)]
public partial class MainWindow : Window, ITestCallBack2, IDisposable //,IDeviceCallBack
{
    private InstanceContext context;
    private DeviceClient deviceClient;

    public MainWindow()
    {
        InitializeComponent();
        context = new InstanceContext(this);
        deviceClient = new DeviceServiceReference.DeviceClient(context);
    }

    public void Dispose()
    {
        deviceClient.Close();
    }

    // Not Wanted
    //public void Test1()
    //{
    //    throw new NotImplementedException();
    //}

    public void Test2()
    {
        throw new NotImplementedException();
    }
}
最佳回答

我对你在此提出的要求并不完全清楚,但我将对帮助任何方面开枪......

因此,在周转基金中,你只能有一个单一的备用接口,这意味着你需要两个服务。 你可以做一些遗产,但这样你就不必重复服务器上的任何东西。 下面是我希望说明如何做到这一点的一个例子。

// A base interface for both services that contains the common methods
[ServiceContract]
public interface ITestService
{
    [OperationContract]
    bool Subscribe();

    [OperationContract]
    bool Unsubscribe();
}

// Service interface for service 1, using callback 1
[ServiceContract(CallbackContract = typeof(ITestCallBack1))]
public interface ITestContract1 : ITestService
{
}

// Callback interface for service 1
public interface ITestCallBack1
{
    [OperationContract(IsOneWay = true)]
    void Test1();
}

// Service interface for service 2, using callback 2
[ServiceContract(CallbackContract = typeof(ITestCallBack2))]
public interface ITestContract2 : ITestService
{
}

// Callback interface for service 2
public interface ITestCallBack2
{
    [OperationContract(IsOneWay = true)]
    void Test2();
}

// This is a base class that contains everything common to the two services
public abstract class TestServiceBase<T> : ITestService
{

    public bool Subscribe()
    {
        // Let s say that after subscribing we will wait for a bit
        // and call back (just an example)
        ThreadPool.QueueUserWorkItem(o =>
                                         {
                                             Thread.Sleep(5000);
                                             RaiseCallback((T) o);
                                         },
                                     OperationContext
                                         .Current
                                         .GetCallbackChannel<T>());
        return true;
    }

    public bool Unsubscribe()
    {
        // Do whatever you need here
        return true;
    }

    // abstract method to raise the callback because the method names
    // are different for the two callback interfaces - notice the overriding
    // method does not need to do anything except call the correctly named method
    protected abstract void RaiseCallback(T callback);
}

// Concrete implementation of TestService1 - you can see that it
// only does whatever is specific for it
public class TestService1 : TestServiceBase<ITestCallBack1>, ITestContract1
{
    // Notice I get the callback1 interface to call the client
    protected override void RaiseCallback(ITestCallBack1 callback)
    {
        callback.Test1();
    }
}

// Concrete implementation of TestService2 - you can see that it
// only does whatever is specific for it
public class TestService2 : TestServiceBase<ITestCallBack2>, ITestContract2
{
    // Notice I get the callback2 interface to call the client
    protected override void RaiseCallback(ITestCallBack2 callback)
    {
       callback.Test2();
    }
}   

这里的服务配置:

<system.serviceModel>
  <services>
    <service name="Demo.TestService1" behaviorConfiguration="NetTcpServiceBehavior">
      <host>
        <baseAddresses>
          <add baseAddress="net.tcp://localhost:9999/TestService1"/>
        </baseAddresses>
      </host>
     <endpoint address="" binding="netTcpBinding" contract="Demo.ITestContract1" bindingConfiguration="NetTcpBindingConfiguration"/>
     <endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" bindingConfiguration="MexBindingConfiguration"/>
    </service>
    <service name="Demo.TestService2" behaviorConfiguration="NetTcpServiceBehavior">
      <host>
        <baseAddresses>
          <add baseAddress="net.tcp://localhost:9999/TestService2"/>
        </baseAddresses>
      </host>
      <endpoint address="" binding="netTcpBinding" contract="Demo.ITestContract2" bindingConfiguration="NetTcpBindingConfiguration"/>
      <endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" bindingConfiguration="MexBindingConfiguration"/>
    </service>
  </services>
  <bindings>
    <netTcpBinding>
      <binding name="NetTcpBindingConfiguration"
               maxConnections="5"
               portSharingEnabled="true">
        <security mode="None">
          <transport protectionLevel="None"/>      
        </security>
      </binding>
      <binding name="MexBindingConfiguration" portSharingEnabled="true">
        <security mode="None">
          <transport protectionLevel="None"/>    
        </security>
      </binding>
    </netTcpBinding>
  </bindings>
  <behaviors>
    <serviceBehaviors>
      <behavior name="NetTcpServiceBehavior">
        <serviceMetadata />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

然后,就客户而言,你需要提及你对反馈意见感兴趣的服务(因此,阁下的《世界森林框架》1将参考文件/《试验服务》1和《世界森林框架》2/《试验服务》2)。

通知你,你可以把测试服务类别中你两个服务所共有的所有逻辑都说得通,只是为了能够说话。 在现实中,你可能不需要这样做——我不知道,在什么情况下,你希望向客户呼吁。

问题回答

暂无回答




相关问题
WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

Access WCF service on same server

I have a .NET website with a WCF service. How do I access the current operations context of my service? One possible work around is to just make a call to the service within the app...but that seems ...

WCF binding error

So I got into work early today and got the latest from source control. When I try to launch our ASP.NET application, I get this exception: "The binding at system.serviceModel/bindings/wsHttpBinding ...

The service operation requires a transaction to be flowed

I am facing strange issue with our WCF service. The same code was working fine until recently we added more OperationContracts(Web Methods). We have common 3 tier architecture. DAL (WCF) BLL Web ...

热门标签