试图在我的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测试客户端访问时,我得到:
错误:如果这是您可以访问的http://localhost:9090/hello (R)通信基础服务,则无法从http://localhost:9090/hello获取元数据,请检查是否已启用指定地址的元数据发布。有关启用元数据发布的帮助,请参阅http://go.microsoft.com/fwlink/?LinkId=65455上的MSDN文档。
元数据交换错误URI:http://localhost:9090/hello
元数据包含无法解析的引用:'http://localhost:9090/hello'‘。
在http://localhost:9090/hello没有能够接收消息的端点侦听。这通常是由不正确的地址或SOAP操作造成的。有关更多细节,请参见InnerException (如果存在)。
无法连接到远程服务器,无法连接,因为目标计算机主动拒绝了127.0.0.1:9090
HTTP获取错误URI:http://localhost:9090/hello下载'http://localhost:9090/hello'‘时出错。无法连接到远程服务器,无法连接,因为目标计算机主动拒绝了127.0.0.1:9090
当我尝试添加服务引用时,我得到:
下载'http://localhost:9090/hello'‘时出错。
无法连接到远程服务器。
由于目标机器主动拒绝连接,因此无法建立连接。
127.0.0.1:9090
元数据包含无法解析的引用:'http://localhost:9090/hello'‘。
在http://localhost:9090/hello上没有可以接受
消息。这通常是由不正确的地址或SOAP操作造成的。有关更多细节,请参见InnerException (如果存在)。
无法连接到远程服务器。
无法连接,因为目标机器主动拒绝了127.0.0.1:9090
如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。。
发布于 2012-03-27 02:20:26
问题是,您正在让ServiceHost立即超出范围。
当代码块超出作用域时,using语句可以方便地进行清理,但是您没有任何地方可以防止这一点。所以从本质上说,你是在打开连接,但是它几乎马上就被处理了.从而关闭了连接。
只要您没有遇到任何权限问题,这种方法就应该适用于您。话虽如此,这只是个演示软件。实际上,您可能不希望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);
}
}https://stackoverflow.com/questions/9882412
复制相似问题