WCF How to enable metadata?

2019-04-04 18:22发布

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.

标签: c# wcf metadata
6条回答
一纸荒年 Trace。
2楼-- · 2019-04-04 18:47

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-04-04 18:56

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.

查看更多
Deceive 欺骗
4楼-- · 2019-04-04 18:57

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
{
查看更多
Evening l夕情丶
5楼-- · 2019-04-04 18:59
  1. Run "\%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -i

  2. Remove Virtual Directory and create it again.

查看更多
老娘就宠你
6楼-- · 2019-04-04 19:03

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"/>
查看更多
Viruses.
7楼-- · 2019-04-04 19:05

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.

查看更多
登录 后发表回答