无法运行第一个WCF服务

时间:2011-05-13 14:42:56

标签: wcf .net-4.0

这是我的第一个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>

我不确定是什么问题。你能指导我吗?

1 个答案:

答案 0 :(得分:0)

我认为你的合同可能需要指向界面而不是具体的实现

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