試圖在我的Win 7系統上運行自託管應用程序,但收效甚微。該應用程序啓動,但我無法從WCF測試客戶端訪問它或通過在VS中添加引用。我已經閱讀了1000篇關於類似問題的文章,但沒有一篇似乎適合。無法訪問服務在非域上的Windows 7自託管WCF應用程序
我這樣做:
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.
您是否嘗試添加MEX行爲? http://stackoverflow.com/questions/6056329/error-cannot-obtain-metadata-from-wcf-service或http://stackoverflow.com/questions/6156461/error-cannot-obtain-metadata-from-http- 172-16-70-1258080-when-using-wcf-cli。你也可能想修復你的.NET軟件包[http://stackoverflow.com/questions/189436/wcf-errors-using-wcftestclient-to-test-a-simple-wcf-web-service] – Nayan 2012-03-27 02:24:09