WCF service使用named pipes。我現在正在這樣做,以在一些WF4活動的設計界面和Visual Studio擴展之間進行溝通。
它很簡單。我無法顯示所有的代碼,因爲它的一些代碼封裝在控制打開和關閉通道的幫助程序中,但定義非常簡單,並且全部以代碼完成。
你只需要定義一個綁定
var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.Transport);
binding.ReceiveTimeout = TimeSpan.FromMinutes(1);
建立頻道
var channelFactory = new ChannelFactory<IServiceInterface>(binding, endpointAddress);
,你必須確保端點地址是保證在客戶端和服務器相同,它們共享相同的進程,但存在於不同的AppDomain中。一個簡單的方法做,這是作用域的地址進程ID ...
private const string AddressFormatString =
"net.pipe://localhost/Company/App/HostType/{0}";
private static string _hostAddress;
public static string HostAddress()
{
if (_hostAddress == null)
_hostAddress = string.Format(
AddressFormatString,
Process.GetCurrentProcess().Id);
return _hostAddress;
}
你有這個(,一個客戶端應用程序域一個在插件的AppDomain)兩個實際的副本,但由於它們都在同一個進程中,主機地址保證在兩者中都是相同的,並且你不會遇到VS同時加載多個VS實例的問題(不要擔心Running Object Table,謝謝)。
我將這個地址代碼保存在基本主機類中。打開主機通道也很容易:
Host = new ServiceHost(this, new Uri(HostAddress()));
var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.Transport);
Host.AddServiceEndpoint(typeof(IServiceInterface), binding, HostAddress());
Host.Open();
確實[這](http://stackoverflow.com/questions/1657863/remoting-and-missing-channel-sinks)幫助? – Daniel 2012-01-30 05:41:33
@丹尼爾 - 謝謝,我已經看到了,它看起來很相關,但沒有看到答案可以幫助我(我幾乎沒有使用過Remoting API)。我的一部分麻煩可能是我的面板控件已經從'Panel'繼承,不能從MarshalByRefObject繼承。此外,'CreateToolWindow2'實際上創建了我的面板實例並將其交給我......不確定是否缺少對象的所有權會使事情變得困難(指的是「......兩端創建通道」)。 。 – 2012-01-30 05:52:51
@Daniel - 實際上,'Panel'確實最終從MarshalByRefObject繼承,所以我想我已經有一些東西在我身邊了...... – 2012-01-30 06:02:48