2011-05-13 88 views
0

这是我的第一个WCF服务,我得到一个错误:无法运行第一WCF服务

Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata. 

代码:

namespace WcfMathServiceLibrary 
{ 
    [ServiceContract] 
    public interface IMath 
    { 
     [OperationContract] 
     double Add(double i, double j); 
     [OperationContract] 
     double Sub(double i, double j); 
     [OperationContract] 
     Complex AddComplexNo(Complex i, Complex j); 
     [OperationContract] 
     Complex SubComplexNo(Complex i, Complex j); 
    } 

    [DataContract] 
    public class Complex 
    { 
     private int _real; 
     private int _imaginary; 

     [DataMember] 
     public int real { get; set; } 

     [DataMember] 
     public int imaginary { get; set; } 

    } 




namespace WcfMathServiceLibrary 
{ 
    public class MathService : IMath 
    { 
     public double Add(double i, double j) 
     { 
      return (i + j); 
     } 

     public double Sub(double i, double j) 
     { 
      return (i - j); 
     } 

     public Complex AddComplexNo(Complex i, Complex j) 
     { 
      Complex result = new Complex(); 
      result.real = i.real + j.real; 
      result.imaginary = i.imaginary + j.imaginary; 
      return result; 
     } 

     public Complex SubComplexNo(Complex i, Complex j) 
     { 
      Complex result = new Complex(); 
      result.real = i.real - j.real; 
      result.imaginary = i.imaginary - j.imaginary; 
      return result; 
     } 
    } 

的Web.Config

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="WcfMathServiceLibrary.MathService"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8732/WcfMathServiceLibrary/MathService/"/> 
      </baseAddresses> 
     </host> 
     <endpoint address="" binding="wsHttpBinding" contract="WcfMathServiceLibrary.MathService"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 

     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" > 
     </endpoint> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- 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> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

我不知道是什么问题。你能指导我吗?

回答

0

我想你的合同可能需要指向接口,而不是具体的实现

<endpoint address="" binding="wsHttpBinding" contract="WcfMathServiceLibrary.IMath"> 
+0

我没有得到你。我需要在地址中填写任何内容吗? – Chris 2011-05-13 15:02:13

+0

不,只是将合同更改为指向IMath接口而不是MathService – 2011-05-14 17:28:19