1

***你能根据我的程序类提供app.config文件吗?

如果可能,请提供 app.config 代码,我明白了你的意思,但对于你所说的,我不知道该怎么做***

此代码不起作用,因为它在到达时导致错误host.open()。请帮我解决问题

public class Program
{
    static void Main(string[] args)
    {
        // Base address
        Uri baseServiceAddress = new Uri("http://localhost:8090/Welcome");

        using (var host = new ServiceHost(typeof(WCFSelfHostedService), baseServiceAddress))
        {
            // Enable MetaData publishing.
            ServiceMetadataBehavior serviceMetaDataBehaviour = new ServiceMetadataBehavior();
            serviceMetaDataBehaviour.HttpGetEnabled = true;
            serviceMetaDataBehaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(serviceMetaDataBehaviour);

            // Open the ServiceHost to start listening for messages. No endpoint are explicitly defined, runtime creates default endpoint.
            host.Open();

            Console.WriteLine("The service is ready at {0} and host at {1}", baseServiceAddress, DateTime.Now.ToString());
            Console.WriteLine("The service and client is running in the same process.");

            WCFSelfHostedService selfHostService = new WCFSelfHostedService();

            Console.Write("Enter your name. : ");
            Console.WriteLine(selfHostService.WelComeMessage(Console.ReadLine()));
            Console.WriteLine("Host is running... Press <Enter> key to stop the service.");
            Console.ReadLine();

            //Close the service.
            host.Close();
        }
    }
}

当我运行此代码时,我收到此错误:

System.ServiceModel.dll 中出现“System.InvalidOperationException”类型的未处理异常

附加信息:服务 'WCFSelfHostedService.WCFSelfHostedService' 的应用程序(非基础设施)端点为零。这可能是因为没有为您的应用程序找到配置文件,或者因为在配置文件中找不到与服务名称匹配的服务元素,或者因为在服务元素中没有定义端点。

app.config的是:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
   <system.serviceModel> 
      <client> 
         <endpoint 
             address ="localhost:8090/Welcome"; 
             binding ="wsHttpBinding" 
             contract ="MyCalculatorService.ISimpleCalculator"> 
         </endpoint> 
      </client> 
   </system.serviceModel> 
</configuration>
4

1 回答 1

2

错误很明显:服务没有定义应用程序端点

要么您有一个未向我们展示的配置文件(如果是这样:请务必展示!),它将配置服务端点 - 或者您需要在代码中设置至少一个服务端点。

app.config因此,要么为发生自托管的应用程序创建一个文件,然后向其中添加如下内容:

<system.serviceModel>
    <services>
        <service name="YourNamespace.WCFSelfHostService" >
           <endpoint name="Default"
               address="http://yourServerName:8088/SomePlace/ServiceName"
               binding="basicHttpBinding"
               contract="YourNamespace.IWCFSelfHostService" />
        </service> 
    </services>
</system.serviceModel>

或者然后更改您的代码以在代码中设置至少一个服务端点 -您调用之前host.Open()

// Base address
Uri baseServiceAddress = new Uri("http://localhost:8090/Welcome");

using (var host = new ServiceHost(typeof(WCFSelfHostedService), baseServiceAddress))
{
   // Enable MetaData publishing.
   ServiceMetadataBehavior serviceMetaDataBehaviour = new ServiceMetadataBehavior();
   serviceMetaDataBehaviour.HttpGetEnabled = true;
   serviceMetaDataBehaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
   host.Description.Behaviors.Add(serviceMetaDataBehaviour);

   // Define AT LEAST one service endpoint
   host.AddServiceEndpoint(typeof(IWCFSelfHostService), new BasicHttpBinding(), baseServiceAddress);

   // Open the ServiceHost to start listening for messages. No endpoint are explicitly defined, runtime creates default endpoint.
   host.Open();

   Console.WriteLine("The service is ready at {0} and host at {1}", baseServiceAddress, DateTime.Now.ToString());
   Console.WriteLine("The service and client is running in the same process.");

   WCFSelfHostedService selfHostService = new WCFSelfHostedService();

   Console.Write("Enter your name. : ");
   Console.WriteLine(selfHostService.WelComeMessage(Console.ReadLine()));
   Console.WriteLine("Host is running... Press <Enter> key to stop the service.");

   Console.ReadLine();

   // Close the service.
   host.Close();
}

更新:app.config您向我们展示的正在定义一个客户- 一个正在调用服务的程序。但是,您需要的app.config是定义事物的SERVICE方面 - 请参阅我的示例。您需要<services>在配置中定义一个或多个,这反过来又会公开至少一个客户端可以连接的应用程序端点。

WCF 始终由服务(在服务器上)和一个(或多个)调用该服务的客户端组成。有关 WCF 体系结构的非常基本的介绍,请参阅这篇文章。该服务在该部分的配置文件中进行配置<services>/<service>,而您的客户端需要在该<client>部分中定义它们的东西。

于 2012-11-24T09:44:54.273 回答