简体   繁体   English

自托管WCF无法访问

[英]Self hosted WCF not reachable

Hi guys I just want a simple WinForm app with one button. 嗨,大家好,我只想要一个带一个按钮的简单WinForm应用程序。 When I press the button i want to start the selfhosted WCF service. 当我按下按钮时,我想启动自托管的WCF服务。 I want to able to connect to this service with for example another client app (winforms) by just adding a service reference. 我希望能够通过添加服务引用,例如使用另一个客户端应用程序(winforms)连接到该服务。

However the solution that I created is not working. 但是,我创建的解决方案无法正常工作。 I can't get connected with adding a service reference to this service. 我无法将服务引用添加到此服务。 I don't actually know what address to call than except the address that I defined in the app.config file. 除了我在app.config文件中定义的地址之外,我实际上不知道要调用的地址。 Any help would be great. 任何帮助都会很棒。

Here is the app.config file. 这是app.config文件。

<configuration>
    <system.serviceModel>
        <services>
            <service name="WindowsFormsApplication11.WmsStatService">
                <endpoint address="http://192.168.0.197:87" binding="basicHttpBinding" 
                    bindingConfiguration="" contract="WindowsFormsApplication11.IWmsStat"/>
            </service>
        </services>
    </system.serviceModel>
</configuration>

And forms code: 并形成代码:

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();
        }      

        private void button1_Click(object sender, EventArgs e)
        {
            _host = new ServiceHost(typeof(WmsStatService));
            _host.Open();
        }
    }

    // Define a service contract.
    [ServiceContract(Namespace = "http://WindowsFormsApplication11")]
    public interface IWmsStat
    {
        [OperationContract]
        string sayHello(string name);
    }

    public class WmsStatService : IWmsStat
    {
        public string sayHello(string name)
        {
            return "hello there " + name + " nice to meet you!";
        }
    }
}

I changed the app.config file. 我更改了app.config文件。 The problem is solved. 问题已经解决了。 Also thanks for the tips and your answers. 也感谢您的提示和答案。 The config is changed to. 配置更改为。

<configuration>
  <system.serviceModel>
    <services>
      <service name="WindowsFormsApplication11.WmsStatService" behaviorConfiguration="mex">
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.0.197:87/" />
          </baseAddresses>
        </host>
        <endpoint address="http://192.168.0.197:87/Test" binding="basicHttpBinding" bindingConfiguration="" contract="WindowsFormsApplication11.IWmsStat" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mex">
          <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

The host should open on http://192.168.0.197:81 as in the config file. 如配置文件中所示,主机应在http://192.168.0.197:81上打开。

So once the host is up and running then, try and broswe to it using the service reference. 因此,一旦主机启动并运行,就可以尝试使用服务参考来浏览主机。

I assume that the address is that your machine, and you don't have anything else on that port address. 我假设该地址就是您的机器,并且该端口地址上没有任何其他地址。 The other things to check are firewalls blocking that port. 要检查的其他事项是防火墙阻止了该端口。

I would not have the service class that implements the service contract (interface) be the form - make it a separate interface, a separate class. 我不会将实现服务协定(接口)的服务类的形式设置为-使它成为一个单独的接口,一个单独的类。 The reasoning behind this is the fact that the service host will have to create (instantiate) one instance of the service class for each request it needs to handle --> make those classes as small as possible and don't bloat them by baggage (like the Winform) that they don't need for their job! 其背后的原因是这样的事实,即服务主机将必须为其需要处理的每个请求创建(实例化)服务类的一个实例->使这些类尽可能小,并且不要因行李而(肿(例如Winform),他们不需要工作!

Then instantiate a ServiceHost inside your Winform - but make that a global member variable of the form! 然后在Winform内实例化一个ServiceHost-但是使它成为表单的全局成员变量! Otherwise, the ServiceHost is gone once your ButtonClick event is finished! 否则,一旦您的ButtonClick事件完成,ServiceHost就消失了!

// Define a service contract.
[ServiceContract(Namespace = "http://WindowsFormsApplication11")]
public interface IWmsStat
{
   [OperationContract]
   string sayHello(string name);
}

public class YourServiceClass : IWmsStat
{
   public string sayHello(string name)
   {
      return "hello there " + name + " nice to meet you!";
   }
}

public partial class Form1 : Form
{
    private ServiceHost _host = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Create a ServiceHost for the CalculatorService type and 
        // provide the base address.
        _host = new ServiceHost(typeof(YourServiceClass));

        // Open the ServiceHostBase to create listeners and start 
        // listening for messages.
        _host.Open();
    }

Don't mix the class that contains the ServiceHost, with the ServiceClass (which will need to be instantiated by the host to satisfy incoming requests) - the Service implementation should be standalone, and as lean as possible! 不要将包含ServiceHost的类与ServiceClass(需要由主机实例化才能满足传入的请求)混合使用-Service实现应独立并且尽可能精简!

Also, it's good practice to follow the Single Responsability Principle - one class should have one job and one job only - don't pack up your whole app logic into a single, huge class - separate out the different jobs into separate classes and compose those together. 同样,遵循“单一责任原则”是一个好习惯-一个班级应该只有一份工作,只有一份工作-不要将整个应用程序逻辑打包到一个庞大的班级中-将不同的工作分成单独的班级并组成一起。

Marc

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

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