C# WCF-控制台托管

C# WCF-控制台托管,c#,wcf,console,localhost,hosting,C#,Wcf,Console,Localhost,Hosting,我只是想知道。。我已经创建了工作WCF服务,其中serwer是一个控制台应用程序。只有当我在VisualStudio中启动主机时,这才有效,对吗?我的意思是,当我尝试从*.exe启动时,它似乎可以工作,但随后我的localhost页面显示空白页面,客户端崩溃 我知道我可以使用IIS,但这是正确的行为(我认为是这样的) 主机 using System; using System.Collections.Generic; using System.Linq; using System.Text; u

我只是想知道。。我已经创建了工作WCF服务,其中serwer是一个控制台应用程序。只有当我在VisualStudio中启动主机时,这才有效,对吗?我的意思是,当我尝试从*.exe启动时,它似乎可以工作,但随后我的
localhost
页面显示空白页面,客户端崩溃

我知道我可以使用IIS,但这是正确的行为(我认为是这样的)

主机

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();
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.ServiceModel;
使用System.ServiceModel.Description;
使用GettingStartedLib;
命名空间GettingStartedHost
{
班级计划
{
静态void Main(字符串[]参数)
{
Uri=新的Uri(“http://localhost:8000/GettingStarted/");
ServiceHost selfHost=新的ServiceHost(typeof(CalculatorService),uri);
尝试
{
AddServiceEndpoint(typeof(ICalculator),新的WSHttpBinding(),“CalculatorService”);
ServiceMetadataBehavior smb=新ServiceMetadataBehavior();
smb.HttpGetEnabled=true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
控制台。WriteLine(“Serwer jest Gotowi!”);
Console.WriteLine(“Naciśnij,作者go zamknąć”);
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
捕获(通信异常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>

尝试以管理员身份运行*.exe

仅当我在VisualStudio中启动主机时,它才起作用 这是对的

绝对不是

我的意思是,当我尝试从*.exe启动时,它似乎可以工作,但是 我的本地主机页面显示空白页面和客户端崩溃

他们的代码没有问题,我试了很多次“很有效”。我看到的唯一缺失的是mex端点和策略版本,但使本地主机进入空白页面并使客户端崩溃似乎并不重要

您如何在浏览器中调用您的服务

如果你这么说的话,我想一切都很好

http://localhost:8000/GettingStarted/
->结果正常

但如果你这样称呼它:

http://localhost:8000/GettingStarted/CalculatorService
->结果为 空白页

这是我的控制台App.config设置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

这是我的服务App.config。我删除了所有不必要的内容,因为我们是以编程方式进行的

<?xml version="1.0" encoding="utf-8" ?>
<configuration>    
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
</configuration>

这是添加服务引用时自动生成的客户端配置:

<?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>


注意:起初,当我在没有管理员权限的情况下直接运行*.exe主机时,我不允许在端口中绑定。我测试了我的客户呼叫服务,它也在工作。我在我的客户端中添加了服务引用。

显示您主机的代码我刚刚发布了它。我没有为主机配置App.config。为什么我要使用它?我在最后用“/”正确调用服务,并使用管理员权限运行服务,但仍然得到空白页,客户端崩溃。当我从*.exe而不是从VS中调用它时,是否会错过一些重要的内容?我复制了与您完全相同的场景。我想你需要在这里添加一些信息。那我就不明白了。与从*.exe运行时相比,从内部运行一切都很好。那是空白页。。1) 是否从调试版本运行主机?2) 您是否在app.config中为主机更改了任何内容(我将其设置为空,因为我假设我从代码运行serwer)3)当我从*.exe启动它时,我没有收到WCF服务器消息说serewer已启动-可以吗?4) 是否有可能主机在app.config不在VS中后无法从库中访问它?我只是不知道发生了什么。我的控制台的app.config有点不同,但它是在我添加servicereference(我更新了它)时自动创建的,但当我将它更改为你的app.config时它似乎不起作用,所以这不是重点。你能回答我最后的问题3)和4)吗?或者某些Windows设置不允许我从*.exe运行它?