我正在尝试创建一个基本的WCF应用程序,但是当我运行它时出现错误:
这是服务:
namespace WcfTranslationLibrary
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface ITranslationService
{
[OperationContract]
string Translate(string value);
// TODO: Add your service operations here
}
}
这是我的班级:
namespace WcfTranslationLibrary
{
// 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 PigLatinTranslator : ITranslationService
{
public string Translate(string value)
{
return string.Format("You entered: {0}", value);
}
}
}
这是我的App.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="WcfTranslationLibrary.PigLatinTranslator">
<endpoint address="" binding="basicHttpBinding" contract="WcfTranslationLibrary.ITranslationService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/WcfTranslationLibrary/PigLatinTranslator/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="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>
这是我使用的控制台应用程序:
namespace ServiceHostConsoleApp
{
class Program
{
static void Main(string[] args)
{
ServiceHost myHost = new ServiceHost(typeof(PigLatinTranslator));
myHost.Open();
Console.WriteLine("Translator running...");
Console.ReadLine();
myHost.Close();
}
}
}
有什么想法吗?
答案 0 :(得分:0)
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8080/hello"); <--url
// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress)) <-- url
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); <-- metadata
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
// Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
host.Open();
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
// Close the ServiceHost.
host.Close();
}
}
}
我认为您错过了一些设置,至少是网址,请查看msdn提供的示例