WCF 如何启用元数据?

发布于 2024-08-18 10:50:46 字数 2956 浏览 2 评论 0原文

我正在尝试让我的 svc 文件在 IIS 下工作。在我的项目中,当我按 F5 时,svc 就开始工作了。所以我知道一切都好,对吗? IIS 除外。

我正在 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" %>

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

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

我做错了什么?

解决方案

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

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

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.

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

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

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

Client: (project: 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)

  <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" %>

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

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风吹短裙飘 2024-08-25 10:50:46

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

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

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

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

First, remove the [DataContract] attribute in Client.

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

Your config should use this endpoint for metadataexchange:

<endpoint address="mex" 
    binding="mexHttpBinding" 
    contract="IMetadataExchange"/>
月光色 2024-08-25 10:50:46

解决方案

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

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

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.

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.

无风消散 2024-08-25 10:50:46

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

[DataContract]
public class Client : ICarePlanActions
{

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

更改此值以引用您的服务的实现!你们的服务实施情况如何?

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

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.

烙印 2024-08-25 10:50:46
  1. 运行 "\%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -i

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

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

  2. Remove Virtual Directory and create it again.

铁轨上的流浪者 2024-08-25 10:50:46

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

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

希望它能正常工作。

Just check the service name in Service.svc file

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

Hope it'll work.

爱已欠费 2024-08-25 10:50:46

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

Ensure that your service namespace, and service class names correspond to the App.config file.

 <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
{
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文