简体   繁体   English

如何在运行时添加WCF服务

[英]How to add wcf service at runtime

How can i add wcf service at runtime in my winform UI. 如何在运行时在Winform UI中添加wcf服务。 I created a wcf service which return running processes of hosted machine. 我创建了一个wcf服务,该服务返回托管计算机的运行进程。 I want to add the hosted machine service in my winform application. 我想在我的winform应用程序中添加托管的机器服务。

You need to change endpoints dynamically at runtime, so You need WCF Discovery . 您需要在运行时动态更改端点,因此您需要WCF Discovery

Structure : 结构体 :

WCF Consumer(s) <---> WCF Discovery Service <---> WCF Service(s)

Implementation : 实现方式:

  1. How to: Implement a Discovery Proxy 如何:实施发现代理
  2. How to: Implement a Discoverable Service that Registers with the Discovery Proxy 如何:实施向发现代理注册的可发现服务
  3. How to: Implement a Client Application that Uses the Discovery Proxy to Find a Service 如何:实施使用发现代理查找服务的客户端应用程序

Topology : 拓扑结构:

  • Start Discovery Service [ Structure BackBone ] 启动发现服务[结构骨干]
  • Start Service(s) [ Every Service will ANNOUNCE its startup to the Discovery Service ] 启动服务(S)每个服务将公布其启动时搜索服务]
  • Start Client(s) [ Every Client will DISCOVER ( FIND & RESOLVE ) Services' endpoints from the Discovery Service ] 启动客户端(图)每个客户会发现 (FIND解析 )从搜索服务服务端点]

Notes : 注意事项:

  • Discovery process uses UDP ( Check your Firewall, It can block connections ) 发现过程使用UDP (检查防火墙,它可以阻止连接)
  • Services MUST announce their startup, thus Self-Hosted services is OK, but IIS-Hosted 5/6 ones is NOT because they started automatically when 1st invoke happends ! 服务必须宣布它们的启动,因此自托管服务是可以的,但是IIS托管 5/6服务不是,因为它们在第一次调用发生时自动启动!

Solving IIS-Hosted 5/6 Issue : 解决由IIS托管的5/6问题:

So that you can start your IIS-Hosted 5/6 services manually without being invoked for the first time 这样您就可以手动启动IIS托管的5/6服务,而无需第一次调用


You can also use WCF Routing Service . 您也可以使用WCF路由服务

BROTHER TIP : 兄弟提示:
Don't go far for a Serverless ( No-BackBone, No-BootleNeck, Fully-Distributed, .. etc ) ideal topology, this'll blowup your head and got you crazy :D 不要为无服务器(No-BackBone,No-BootleNeck,Fully-Distributed等)的理想拓扑走的太远,这会让您大吃一惊,让您发疯:D

For a beginner, I suggest you this tutorial [ WCF Tutorials ] 对于初学者,我建议您使用本教程[ WCF教程 ]

not sure what you are trying to do here. 不知道您要在这里做什么。 But you need to know two things to call the WCF service 1) Service Contract 2) End Point. 但是您需要知道两点才能调用WCF服务1)服务合同2)端点。 Now there is no escaping from Service Contract as you need to know what all operations you can consume. 现在,您无需从服务合同中逃脱,因为您需要了解可以消耗的所有操作。 However, with WCF 4 there is a new feature called WCF discovery which helps you determine the end point dynamically ie at RunTime. 但是,在WCF 4中,有一个称为WCF发现的新功能,可以帮助您动态地(即在运行时)确定终点。 Refer to following link http://msdn.microsoft.com/en-us/library/dd456791.aspx 请参阅以下链接http://msdn.microsoft.com/en-us/library/dd456791.aspx

If I understand you question properly you need some code that will add service in run-time without using any configuration in *.config file and *.svc files. 如果我理解正确,您需要一些代码,这些代码将在运行时添加服务,而无需在* .config文件和* .svc文件中使用任何配置。

See that sample: 看到该示例:

    Uri baseAddress = new Uri("http://localhost:8080/hello");
    // Create the ServiceHost.
    using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
    {
        // Enable metadata publishing.
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(smb);

    // Open the ServiceHost to start listening for messages. Since
    // no endpoints are explicitly configured, the runtime will create
    // one endpoint per base address for each service contract implemented
    // by the service.
    host.Open();

    Console.WriteLine("The service is ready at {0}", baseAddress);
    Console.WriteLine("Press <Enter> to stop the service.");
    Console.ReadLine();

    // Close the ServiceHost.
    host.Close();
}

It creates self-hosted service in console app. 它在控制台应用程序中创建自托管服务。

http://msdn.microsoft.com/en-us/library/ms731758.aspx http://msdn.microsoft.com/en-us/library/ms731758.aspx

Is that what you asked? 那是你问的吗?

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

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