问题
I created a basic calculator service called MiniCalc that only has two operations. Add and Mul, and hosted it in a Console Application.
using(ServiceHost host = new ServiceHost(typeof(MiniCalcService.Service),
new Uri("http://localhost:8091/MiniCalcService")))
{
host.AddServiceEndpoint(typeof(MiniCalcService.IService),
new BasicHttpBinding(),
"Service");
host.Open();
Console.Write("Press ENTER key to terminate the MiniCalcHost . . . ");
}
Then I created a console application to consume the service and created the proxy manually by creating a proxy class and then created a ChannelFactory to invoke the service.
EndpointAddress ep = new EndpointAddress("http://localhost:8091/MiniCalcService/Service");
IService proxy = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(),ep);
I was able to invoke the service contract properly and retrieve the result as expected.
Now I wanted to create the proxy using the Add Service Reference
.
I get the following error when I click Go in the Add Service Reference window
There was an error downloading 'http://localhost:8091/MiniCalcService/Service'.
The request failed with HTTP status 400: Bad Request.
Metadata contains a reference that cannot be resolved: 'http://localhost:8091/MiniCalcService/Service'.
Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:8091/MiniCalcService/Service. The client and service bindings may be mismatched.
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..
If the service is defined in the current solution, try building the solution and adding the service reference again.
What am I missing or doing wrong?
回答1:
Enable Metadata exchange behavior in your ServiceHost.
using(ServiceHost host = new ServiceHost(typeof(MiniCalcService.Service),
new Uri("http://localhost:8091/MiniCalcService")))
{
host.AddServiceEndpoint(typeof(MiniCalcService.IService),
new BasicHttpBinding(),
"Service");
//Enable metadata exchange
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb);
host.Open();
Console.Write("Press ENTER key to terminate the MiniCalcHost . . . ");
}
http://wcftutorial.net/WCF-Self-Hosting.aspx
回答2:
Since you don't have a .svc I think you must have this in your service .config:
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
<serviceActivations>
<add relativeAddress="Service.svc" service="MiniCalcService.Service" />
</serviceActivations>
</serviceHostingEnvironment>
After that you need an option to allow service metadata:
<serviceMetadata httpGetEnabled="true" />
This is a bit more complicated so I advise you tu create a new WCF Service in a new Solution then you can see how this configuration looks like. So you only need to make some copy/paste configurations.
After that point to:
http://localhost:8091/MiniCalcService/Service.svc
来源:https://stackoverflow.com/questions/9852019/unable-to-generate-a-proxy-for-a-self-hosted-service-with-add-service-reference