C# WCF 如何启用元数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2089304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:25:13  来源:igfitidea点击:

WCF How to enable metadata?

c#wcfmetadata

提问by Martijn

I am trying to get my svc file working under IIS. In my project, when I press F5 I got the svc working. So I know everything is okay, right? Except for IIS.

我试图让我的 svc 文件在 IIS 下工作。在我的项目中,当我按 F5 时,我让 svc 工作。所以我知道一切都很好,对吧?除了 IIS。

I am working on a Windows XP Pro machine and in IIS I've added a virtual directory.

我在 Windows XP Pro 机器上工作,在 IIS 中我添加了一个虚拟目录。

Here's my code: IcarePlanActions (project: A)

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

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

Client: (project: A)

客户:(项目: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 (project: B)

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

CarePlan.svc

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

When I run this service (which is on IIS) with wfctestclient I get this error

当我使用 wfctestclient 运行此服务(位于 IIS 上)时,出现此错误

Error: Cannot obtain Metadata from http://localhost/CarePlanService/CarePlan.svcIf 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.

错误:无法从http://localhost/CarePlanService/CarePlan.svc获取元数据 如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。

What am I doing wrong?

我究竟做错了什么?

SOLUTION

解决方案

I didn't get the service working under IIS. First I manually create a virtual directory and pointed to the directiry where the svc is located. This didn't work. I don't know why.

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

Then I went to Visual Studio and changed the server setting (Right mouse on the project, properties, tab Web, click Use local IIS Web Serverand click Create Virtual Directory. When I did this, it worked under IIS with the code above.

然后我转到 Visual Studio 并更改服务器设置(在项目上右键单击,属性,选项卡 Web,单击Use local IIS Web Server并单击Create Virtual Directory。当我这样做时,它在 IIS 下使用上面的代码工作。

采纳答案by Martijn

SOLUTION

解决方案

I didn't get the service working under IIS. First I manually create a virtual directory and pointed to the directiry where the svc is located. This didn't work. I don't know why.

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

Then I went to Visual Studio and changed the server setting (Right mouse on the project, properties, tab Web, click Use local IIS Web Server and click Create Virtual Directory. When I did this, it worked under IIS with the code above.

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

回答by Alex

You reference the data contract as the implementation of the service. That's invalid.

您将数据契约引用为服务的实现。那是无效的。

[DataContract]
public class Client : ICarePlanActions
{

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

Change this to reference the implementation of your service! What does the implementation of your service look like?

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

Maybe you should just remove the DataContract attribute from the Client class. Also this is not your "Client", it is the implementation of the service.

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

回答by Fernando Cardenas

First, remove the [DataContract] attribute in Client.

首先,删除 Client 中的 [DataContract] 属性。

Then, recompile and make sure that you have the WcfServiceLibrary.dll in the /bin directory for your virtual directory.

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

Your config should use this endpoint for metadataexchange:

您的配置应使用此端点进行元数据交换:

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

回答by Rooz

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

  2. Remove Virtual Directory and create it again.

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

  2. 删除虚拟目录并重新创建。

回答by user3653166

Just check the service name in Service.svcfile

只需检查Service.svc文件中的服务名称

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

Hope it'll work.

希望它会起作用。

回答by Demian Berisford-Maynard

Ensure that your service namespace, and service classnames correspond to the App.configfile.

确保您的服务命名空间和服务名称对应于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
{