尝试在控制台应用程序中创建简单的自主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中:
<?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>
有例外:
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'.
问题在哪里
答案 0 :(得分:0)
您好,请尝试以下代码,并从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);
}
}
}
}
}