问题
I am just wondering.. I've created working WCF Service, where serwer is a console application. This works only when I launch the host within the Visual Studio, is this right? I mean when I try to launch in from *.exe, it seems to work, but then my localhost
page shows blank page and client crashes.
I know I can use IIS, but is that correct behaviour (as I think it is)?
Host
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Description;
using GettingStartedLib;
namespace GettingStartedHost
{
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:8000/GettingStarted/");
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), uri);
try
{
selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("Serwer jest gotowy!");
Console.WriteLine("Naciśnij <ENTER>, by go zamknąć.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
catch (CommunicationException e)
{
Console.WriteLine("Wystąpił wyjątek: {0}",e.Message);
selfHost.Abort();
}
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICalculator" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/GettingStarted/CalculatorService"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICalculator"
contract="ServiceReference1.ICalculator" name="BasicHttpBinding_ICalculator" />
</client>
</system.serviceModel>
</configuration>
回答1:
This works only when I launch the host within the Visual Studio, is this right?
Absolutely NO!
I mean when I try to launch in from *.exe, it seems to work, but then my localhost page shows blank page and client crashes.
Their is nothing wrong with your code and I tried it multiple times "it works". The only thing I saw missing are mex endpoint and policy version but it seems not important to cause your localhost into blank page and make your client crashes.
How did you call your service in the browser?
If you call it this way think every thing is fine.
http://localhost:8000/GettingStarted/
-> Result is okay
But if you call it like this:
http://localhost:8000/GettingStarted/CalculatorService
-> Result is blank page
This is my Console App.config setting.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
This is my Service App.config. I remove all not unnecessary since we doing it programmatically.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
</configuration>
This is my auto generated client config upon Adding Service Reference:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculator" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/GettingStarted/CalculatorService"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator"
contract="ServiceReference1.ICalculator" name="WSHttpBinding_ICalculator">
<!-- This is new to me and I don't know why it is here??? hmmm... I don't have any idea with this -->
<identity>
<userPrincipalName value="myDomainName@Domain.com" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
Note : At first when I ran my *.exe host directly without administrator privilege I doesn't allow me to bind in port. And I tested my client to call the service and it is also working. I added service reference in my client here.
回答2:
Try to run your *.exe as Administrator.
来源:https://stackoverflow.com/questions/31958241/wcf-console-hosting