gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:13:19 25 4
gpt4 key购买 nike

尝试在我的 Win 7 系统上运行自托管应用程序,但收效甚微。该应用程序启动,但我无法从 WCF 测试客户端或通过在 VS 中添加引用来访问它。我已经阅读了大约 1000 篇关于类似问题的帖子,但似乎没有一个解决方案适合。

我这样做了:

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

然后是:

netsh http add iplisten ipaddress=0.0.0.0:9090

这是执行

的代码
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;
}
}
}

当我尝试从 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. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.
WS-Metadata Exchange Error URI: http://localhost:9090/hello
Metadata contains a reference that cannot be resolved: 'http://localhost:9090/hello'.
There was no endpoint listening at http://localhost:9090/hello that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:9090
HTTP GET Error URI: http://localhost:9090/hello There was an error downloading '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

当我尝试添加服务引用时,我得到:

There was an error downloading '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
Metadata contains a reference that cannot be resolved: 'http://localhost:9090/hello'.
There was no endpoint listening at http://localhost:9090/hello that could accept the
message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Unable to connect to the remote server
No connection could be made because the target machine actively refused it 127.0.0.1:9090
If the service is defined in the current solution, try building the solution and adding the service reference again.

最佳答案

问题是您让 ServiceHost 立即超出范围。

当该代码块超出范围时,using 语句可以方便地进行清理,但您没有任何措施可以防止这种情况发生。所以本质上你是在打开连接,但它几乎是立即被处理掉的……这会关闭连接。

只要您没有遇到任何权限问题,这种方法应该适合您。也就是说,这只是演示软件。实际上,您可能不希望 WCF 服务直接绑定(bind)到表单,而是在应用程序级别定义。

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);
}
}

关于c# - 无法在非域上访问服务 Windows 7 自托管 WCF 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9882412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com