WCF-控制台托管 [英] WCF- console hosting

查看:304
本文介绍了WCF-控制台托管的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

I know I can use IIS, but is that correct behaviour (as I think it is)?

主机

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();
            }
        }
    }
}

strong> App.config

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>


推荐答案


我在Visual Studio中启动主机,是
吗?

This works only when I launch the host within the Visual Studio, is this right?

绝对不!


我的意思是当我尝试从* .exe启动时,它似乎工作,但然后
我的localhost页面显示空白页面和客户端崩溃。 / p>

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.

他们对你的代码没有问题,我多次尝试它工作。我只看到缺少的是mex端点和策略版本,但它似乎并不重要导致您的本地主机到空白页,并使您的客户端崩溃。

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.

如果以这种方式打电话,就认为一切都很好。

If you call it this way think every thing is fine.


http:// localhost:8000 / GettingStarted / - >结果是好的

http://localhost:8000/GettingStarted/ -> Result is okay

但是如果你这样调用:


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

http://localhost:8000/GettingStarted/CalculatorService -> Result is blank page

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

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>

这是我的Service App.config。我删除所有没有必要,因为我们以编程方式。

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.

这篇关于WCF-控制台托管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆