【问题标题】:How to call a method in WPF application from a WCF Service hosted by the same WPF application?如何从同一 WPF 应用程序托管的 WCF 服务调用 WPF 应用程序中的方法?
【发布时间】:2014-08-10 14:50:50
【问题描述】:

我创建了一个托管在 WPF 应用程序中的 WCF 服务。一切正常。我的客户可以毫无问题地访问 WCF 服务。

现在,当客户端调用我的服务中的方法时,例如:SetData(MyDataObject data),我希望该方法将“数据”对象发送回 WPF 主机进行处理。

我怎么把它连接起来???

我的服务托管在 WPF 应用程序中:

host = new ServiceHost(typeof(WCFTestService.Service1), new Uri("http://localhost:8733/WCFTestService/"));

// Service host is opened on the UI thread
host.AddServiceEndpoint(typeof(WCFTestService.IService1), new BasicHttpBinding(), "");

// Enable metadata exchange
ServiceMetadataBehavior smb = new ServiceMetadataBehavior() { HttpGetEnabled = true };
host.Description.Behaviors.Add(smb);

// Enable exeption details
ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.IncludeExceptionDetailInFaults = true;

host.Open();

我的 WCF 服务方法:

public bool SetData(MyDataObject data)
{
   //HOW DO I get MyDataObject to the WPF Host???
   // something like: host.UpdateData(data) <-- callback method???
   return true;
}

【问题讨论】:

    标签: wpf wcf


    【解决方案1】:

    我跑题了,但是表示层和服务层如此紧密耦合是否有特殊原因?我认为您可以将 MyDataObject 的内容添加到您创建的数据传输对象中,或者持久化 MyDataObject(可能是暂时的),然后让 WPF 应用程序/主机通过同一 wcf 服务公开的方法调用来访问它。根据您提供的数据,我可能会这样做。

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 2018-05-17
      相关资源
      最近更新 更多