2010-01-18 54 views
12

我想让我的svc文件在IIS下工作。在我的项目中,当我按F5时,我得到了svc的工作。所以我知道一切都好,对不对?除IIS外。WCF如何启用元数据?

我正在使用Windows XP Pro计算机,并在IIS中添加了一个虚拟目录。

这里是我的代码: IcarePlanActions(项目:A)

namespace WcfServiceLibrary 
{ 
    [ServiceContract] 
    public interface ICarePlanActions 
    { 
     [OperationContract] 
     List<string> GetAllClients(); 
    } 
} 

客户:(项目:A)

namespace WcfServiceLibrary 
{ 
    public class Client : ICarePlanActions 
    { 
     public List<string> GetAllClients() 
     { 
      List<string> clients = new List<string>(); 
      clients.Add("Hendrik de Jong"); 
      clients.Add("Miep de Berg"); 
      clients.Add("Jaap Jongeneel"); 
      clients.Add("Joop Prakman"); 
      clients.Add("Pieter Schaakman"); 

      return clients; 

     } 
    } 
} 

Web.config文件(项目:B)

<configuration> 
    <system.serviceModel> 
     <services> 
     <service behaviorConfiguration="CarePlanService.Service1Behavior" 
      name="WcfServiceLibrary.Client"> 
      <endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary.ICarePlanActions"> 
      <identity> 
       <dns value="localhost" /> 
      </identity> 
      </endpoint> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
     </services> 
     <behaviors> 
     <serviceBehaviors> 
      <behavior name="CarePlanService.Service1Behavior"> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 
     </serviceBehaviors> 
     </behaviors> 
    </system.serviceModel> 
    </configuration> 

CarePlan.svc

<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceLibrary.Client" %> 

当我运行此服务(这是对IIS)与wfctestclient我得到这个错误

Error: Cannot obtain Metadata from http://localhost/CarePlanService/CarePlan.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.

我在做什么错?

SOLUTION

我没有得到服务IIS下工作。首先,我手动创建一个虚拟目录,并指向svc所在的directiry。这没有奏效。我不知道为什么。

然后我去了Visual Studio和更改服务器设置(鼠标右键的项目,属性,标签网页,单击Use local IIS Web Server并单击Create Virtual Directory。当我这样做,它的工作IIS下上面的代码。

+0

编辑标题 - 这与C#编程语言无关。 – 2010-01-18 21:35:39

+1

我没有这样使用过WCF,但是你应该把你的服务实现放在datacontract里面吗?如果是这样,那么合同中就没有定义数据库。只是看起来很奇怪。但我从来没有在IIS托管... – magnus 2010-01-18 22:22:04

+0

@Martijn:真正愚蠢的问题:当你试图使用WcfTestClient连接,是网站吗?如果你做了这里提出的修改(删除[DataContract],为mex端点使用“mexHttpBinding”),我完全没有困难获取元数据。但是,你必须做一个“浏览器查看”上的* .svc文件首先,为了启动内置的Web服务器 - 然后你可以用你的WcfTestClient – 2010-01-19 07:08:24

回答

5

SOLUTION

我没有得到服务IIS下工作。首先,我手动创建一个虚拟目录,并指向svc所在的directiry。这没有奏效。我不知道为什么。

然后我去了Visual Studio并更改了服务器设置(右键单击项目,属性,选项卡Web,单击使用本地IIS Web服务器并单击创建虚拟目录。当我这样做时,它使用上面的代码在IIS下工作。

+0

这为我工作,所以这意味着它在IIS服务器,但不是在Visual Studio开发服务器。你知道为什么? – superachu 2014-07-22 14:52:45

2

您引用数据合同作为服务的实现。这是无效的。

[DataContract] 
public class Client : ICarePlanActions 
{ 

<service behaviorConfiguration="CarePlanService.Service1Behavior" 
     name="WcfServiceLibrary.Client"> 

更改为引用您的服务的实现!什么是您的服务的实现是什么样子?

也许你应该从Client类中删除DataContract属性。另外这不是你的“客户”,它是服务的实施。

+0

我编辑了我的startpost。你是这个意思吗? – Martijn 2010-01-19 07:41:58

+0

是的。我正是这个意思。 – Alex 2010-01-20 13:25:15

6

首先,删除客户端中的[DataContract]属性。

然后,重新编译并确保您的虚拟目录的/ bin目录中有WcfServiceLibrary.dll。

你的配置应该使用这个端点元数据交换:

<endpoint address="mex" 
    binding="mexHttpBinding" 
    contract="IMetadataExchange"/> 
+0

我改变了我的文件,但我仍然没有得到它的工作。我修改了我的startpost。 – Martijn 2010-01-19 07:36:39

0
  1. 运行"\%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -i

  2. 删除虚拟目录,然后再次创建。

0

Service.svc文件只是检查服务名称

<ServiceHost Language="C#" Debug="true" Service="SvcCalculator.**Calculator**" 
      CodeBehind="**Calculator.svc.cs**" %> 

希望它会工作。

0

确保您的服务命名空间和服务名称对应于的App.config文件。

<system.serviceModel> 
    <services> 
    <service name="WcfCustomLibrary.Service1"> 
    <host> 
     <baseAddresses> 
     <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary/Service1/" /> 
     </baseAddresses> 
    </host> 
    </service> 
    </services> 
</system.serviceModel> 


namespace WcfCustomLibrary 
{ 
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. 
public class Service1 : IService1 
{