简体   繁体   English

WCF自托管控制台应用程序中缺少合同

[英]Missing contract in WCF self hosted console application

Trying to make simple selfhosted WCF service in console application: 尝试在控制台应用程序中制作简单的自托管WCF服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace SupportSrvWCF
{


    [ServiceContract]
    public interface IA
    {
        [OperationContract]
        int LogIt(int id, string data, ref int level);
    }


    public class SupportServicee : IA
    {
        public int LogIt(int id, string data, ref int level)
        {

            return 0;
        }

    }

    class Program
    {




        static void Main(string[] args)
        {

            try
            {
                ServiceHost selfHost = new ServiceHost(typeof(SupportServicee));
                selfHost.Open();



                Console.WriteLine("The service is running. Press any key to stop.");
                Console.ReadKey();
            }
            catch (Exception e)
            {

                Console.WriteLine("An error occurred: '{0}'", e);
            }

        }
    }
}

App.config: App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>



  <system.serviceModel>
    <services>


      <service name="SupportSrvWCF.SupportServicee">
        <endpoint address="" binding="basicHttpBinding" contract="SupportSrvWCF.IA ">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8002/WCFService1" />
          </baseAddresses>
        </host>
      </service>




    </services>

    <behaviors>
      <serviceBehaviors>
        <!--<behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>-->
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
        multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

</configuration>

Have exception: 有例外:

An error occurred: 'System.InvalidOperationException: The contract name 'Support
SrvWCF.IA ' could not be found in the list of contracts implemented by the servi
ce 'SupportServicee'.

Where is problem 问题出在哪里

hello try the following code instead and delete the service configuration from app.config 你好,请尝试以下代码,然后从app.config中删除服务配置

 using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace SupportSrvWCF { [ServiceContract] public interface IA { [OperationContract] int LogIt(int id, string data, ref int level); } public class SupportServicee : IA { public int LogIt(int id, string data, ref int level) { return 0; } } class Program { static void Main(string[] args) { using (ServiceHost selfHost = new ServiceHost(typeof(SupportServicee),new Uri("http://localhost:8080/Myservice"))) { try { selfHost.AddServiceEndpoint(typeof(IA),new BasicHttpBinding(),"basic"); selfHost.Open(); Console.WriteLine("The service is running. Press any key to stop."); Console.ReadKey(); } catch (Exception e) { Console.WriteLine("An error occurred: '{0}'", e); } } } } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM