wcf - 当我在浏览器中输入 url 时,自托管 WCF 服务不起作用?

标签 wcf c#-3.0

我是 WCF 的新手。我做了一个简单的自托管服务并添加了app.config但是当我在浏览器中输入地址时,它没有显示我们创建服务时获得的服务页面http://localhost:8067/WCFService它没有像我们运行服务时显示的那样显示服务。

但是当我尝试在 public static void main 中添加基础服务时而不是 app.config它工作正常m没有得到yy??谁能帮帮我吗?

以下是app.config文件手动添加:

<configuration>     
   <system.serviceModel>     
      <services>     
         <service name="SelfHostedWCFService.WCFService">     
            <endpoint
                address="http://localhost:8067/WCFService"
                binding="wsHttpBinding"
                contract="SelfHostedWCFService.IWCFService">     
            </endpoint>     
         </service>     
      </services>     
   </system.serviceModel>     
</configuration> 

以下是Program.cs :
static void Main(string[] args)     
{     
    ServiceHost host = new ServiceHost(typeof(SelfHostedWCFService.WCFService));
    host.Open();     
    Console.WriteLine("Server is Running...............");      
    Console.ReadLine();
}

以下是手动添加的接口(interface)文件:
namespace SelfHostedWCFService     
{      
    [ServiceContract]     
    interface IWCFService      
    {     
        [OperationContract]     
        int Add(int a, int b);      
        [OperationContract]     
        int Sub(int a, int b);      
        [OperationContract]     
        int Mul(int a, int b);
    }     
} 

以下是service.cs文件手动添加:
namespace SelfHostedWCFService
{     
    class WCFService:IWCFService     
    {     
         public int Add(int a, int b) { return (a + b); }      
         public int Sub(int a, int b) { return (a - b); }      
         public int Mul(int a, int b) { return (a * b); } 
    }     
} 

我的 app.config 有问题吗?还是其他概念??

最佳答案

乍一看一切似乎都很好 - 你是 确定 服务没有运行??

如果没有发布任何元数据,您将无法使用 WCF Test Client 测试服务,也不能为它生成客户端代理......

因此,我建议将服务元数据发布添加到您的服务中,这样做,我能够测试您的代码并且它完美地工作。

要添加元数据,请将您的配置更改为:

<configuration>     
   <system.serviceModel>     
      <behaviors>
          <serviceBehaviors>
             <behavior name="Metadata">
                <serviceMetadata />
             </behavior>
          </serviceBehaviors>
      </behaviors>
      <services>     
         <service name="SelfHostedWCFService.WCFService" behaviorConfiguration="Metadata">     
            <endpoint
                address="http://localhost:8067/WCFService"
                binding="wsHttpBinding"
                contract="SelfHostedWCFService.IWCFService" />     
            <endpoint address="http://localhost:8067/WCFService/mex"
                      binding="mexHttpBinding" contract="IMetadataExchange" />     
         </service>     
      </services>     
   </system.serviceModel>     
</configuration> 

即使使用此配置,导航到 URL 时也不会看到任何服务页面 - 但服务已启动并正在运行 - 只需使用 WCF Test Client亲眼看看!

关于wcf - 当我在浏览器中输入 url 时,自托管 WCF 服务不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7541925/

相关文章:

c# - WCF ServiceActivationException 混淆

WCF 4 REST 服务无法返回 StatusDescription,仅返回 StatusCode

c# - C#中的滑动窗口算法

linq - 在 Linq to SQL 中将字符串转换为枚举

c# - .net 库不从 app.config 中选择 key

c# - 在 WCF 服务中使用大量数据后清理内存

c# - Visual Studio : How can I get my test project to start the web project it depends on

C# - 如何在其末尾剪切字符串以适合 div?

mongodb c# 有条件地组合集合操作

wcf - 在 Silverlight 应用程序中使用 datacontractserializer 进行反序列化的性能非常慢