简体   繁体   English

无法在非域上访问服务Windows 7自托管WCF应用程序

[英]Can't access service Windows 7 self-hosted WCF app on a non-domain

Trying to run a self-hosted app on my Win 7 system, with little success. 试图在我的Win 7系统上运行自托管应用程序,但收效甚微。 The app starts up but I can't access it from WCF Test Client or by adding a reference in VS. 该应用程序启动但我无法从WCF测试客户端访问它或通过在VS中添加引用。 I've read what seems like 1000 posts about similar problems, but none of the solutions seem to fit. 我已经阅读了类似问题的1000个帖子,但似乎没有一个解决方案适合。

I did this: 我这样做了:

netsh http add urlacl url=http://+:9090/hello user=LocalPC\UserName

And then this: 然后这个:

netsh http add iplisten ipaddress=0.0.0.0:9090

Here's the code to do the 这是执行此操作的代码

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
        Uri baseAddress = new Uri("http://localhost:9090/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);

            // Add MEX endpoint
            host.AddServiceEndpoint(
              ServiceMetadataBehavior.MexContractName,
              MetadataExchangeBindings.CreateMexHttpBinding(),
              "mex");

            // Add application endpoint
            host.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), "");                

            // 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.
            try
            {
                host.Open();
            }
            catch (Exception excep)
            {
                string s = excep.Message;
            }
        }
    }

When I try to access from WCF Test Client I get: 当我尝试从WCF测试客户端访问时,我得到:

Error: Cannot obtain Metadata from http://localhost:9090/hello If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. 错误:无法从http:// localhost:9090 / hello获取元数据如果这是您有权访问的Windows(R)Communication Foundation服务,请检查您是否已在指定地址启用元数据发布。 For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455 . 有关启用元数据发布的帮助,请参阅http://go.microsoft.com/fwlink/?LinkId=65455上的MSDN文档。
WS-Metadata Exchange Error URI: http://localhost:9090/hello WS-Metadata Exchange错误URI: http:// localhost:9090 / hello
Metadata contains a reference that cannot be resolved: 'http://localhost:9090/hello'. 元数据包含无法解析的引用:'http:// localhost:9090 / hello'。
There was no endpoint listening at http://localhost:9090/hello that could accept the message. http:// localhost:9090 / hello上没有可以接受该消息的端点。 This is often caused by an incorrect address or SOAP action. 这通常是由错误的地址或SOAP操作引起的。 See InnerException, if present, for more details. 有关更多详细信息,请参阅InnerException(如果存在)。
Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:9090 无法连接到远程服务器无法建立连接,因为目标计算机主动拒绝它127.0.0.1:9090
HTTP GET Error URI: http://localhost:9090/hello There was an error downloading 'http://localhost:9090/hello'. HTTP GET错误URI: http:// localhost:9090 / hello下载'http:// localhost:9090 / hello'时出错。 Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:9090 无法连接到远程服务器无法建立连接,因为目标计算机主动拒绝它127.0.0.1:9090

When I try to add a service reference I get: 当我尝试添加服务引用时,我得到:

There was an error downloading 'http://localhost:9090/hello'. 下载'http:// localhost:9090 / hello'时出错。
Unable to connect to the remote server 无法连接到远程服务器
No connection could be made because the target machine actively refused it 无法建立连接,因为目标计算机主动拒绝它
127.0.0.1:9090 127.0.0.1:9090
Metadata contains a reference that cannot be resolved: 'http://localhost:9090/hello'. 元数据包含无法解析的引用:'http:// localhost:9090 / hello'。
There was no endpoint listening at http://localhost:9090/hello that could accept the 没有端点侦听http:// localhost:9090 / hello可以接受
message. 信息。 This is often caused by an incorrect address or SOAP action. 这通常是由错误的地址或SOAP操作引起的。 See InnerException, if present, for more details. 有关更多详细信息,请参阅InnerException(如果存在)。
Unable to connect to the remote server 无法连接到远程服务器
No connection could be made because the target machine actively refused it 127.0.0.1:9090 无法建立连接,因为目标计算机主动拒绝它127.0.0.1:9090
If the service is defined in the current solution, try building the solution and adding the service reference again. 如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。

The problem is that you are letting the ServiceHost go out of scope immediately. 问题是你让ServiceHost立即超出范围。

The using statement is there as a convenience to do cleanup when that block of code goes out of scope, but you have nothing in place to prevent that. 当该代码块超出范围时,using语句可以方便地进行清理,但是没有任何措施可以防止这种情况发生。 So in essence you are opening the connection, but then it is being disposed almost instantly... which closes the connection. 所以从本质上讲,你是打开连接,但它几乎立即被处理......这将关闭连接。

So long as you don't run into any permissions issues, this approach should work for you. 只要您不遇到任何权限问题,这种方法应该适合您。 That being said, this is just demo-ware. 话虽如此,这只是演示软件。 In reality you probably don't want your WCF service tied directly to your form, but rather defined at the application level. 实际上,您可能不希望将WCF服务直接绑定到表单,而是在应用程序级别定义。

public partial class WcfHost : Form
{
    private ServiceHost _svcHost;
    private Uri _svcAddress = new Uri("http://localhost:9001/hello");

    public WcfHost()
    {
        _svcHost = new ServiceHost(typeof(HelloWorldService), _svcAddress);

        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
        _svcHost.Description.Behaviors.Add(smb);

        InitializeComponent();

        FormClosing += WcfHost_FormClosing;
    }

    private void WcfHost_Load(object sender, EventArgs e)
    {
        try
        {
            _svcHost.Open(TimeSpan.FromSeconds(10));
            lblStatus.Text = _svcHost.State.ToString();
        }
        catch(Exception ex)
        {
            lblStatus.Text = ex.Message;
        }            
    }

    void WcfHost_FormClosing(object sender, FormClosingEventArgs e)
    {
        _svcHost.Close();

        lblStatus.Text = _svcHost.State.ToString();
    }
}

[ServiceContract]
public interface IHelloWorldService
{
    [OperationContract]
    string SayHello(string name);
}

public class HelloWorldService : IHelloWorldService
{
    public string SayHello(string name)
    {
        return string.Format("Hello, {0}", name);
    }
}

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

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