gpt4 book ai didi

c# - 自托管 WCF 服务

转载 作者:行者123 更新时间:2023-11-30 20:03:22 25 4
gpt4 key购买 nike

我正在考虑在我的应用程序中创建一个自托管的 WCF 服务。我正在尝试按照 Microsoft 站点上的示例进行操作 http://msdn.microsoft.com/en-us/library/ms731758(v=vs.100).aspx但我遇到了问题。

当程序运行时,我可以进入网页,它说我可以运行 svcutil.exe 来生成客户端类,或者当我进入 WCF 测试客户端时,教程说我收到错误

Service metadata may not be accessible. Make sure your service is running and exposing metadata.

从 svcutil.exe 我得到以下错误:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>svcutil http://localhost:
6525/hello
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1]
Copyright (c) Microsoft Corporation. All rights reserved.

Attempting to download metadata from 'http://localhost:6525/hello' using WS-Meta
data Exchange or DISCO.
Generating files...
Warning: No code was generated.
If you were trying to generate a client, this could be because the metadata docu
ments did not contain any valid contracts or services
or because all contracts/services were discovered to exist in /reference assembl
ies. Verify that you passed all the metadata documents to the tool.

Warning: If you would like to generate data contracts from schemas make sure to
use the /dataContractOnly option.

下面是我的 WCF 应用程序的代码

public interface IHelloWorldService
{
[OperationContract]
string SayHello(string firstName, string lastName);
}

static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:6525/hello");

using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);

host.Open();

Console.WriteLine("The service is ready at: {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service");
Console.ReadLine();
host.Close();

}
}

public class HelloWorldService : IHelloWorldService
{
public string SayHello(string firstName, string lastName)
{
return string.Format("Hello {0} {1}", firstName, lastName);
}
}

感谢您提供的任何帮助

最佳答案

乍一看很难找出问题所在,但当我测试你的代码时,我发现你的代码中缺少某些东西。你忘了向客户公开你的契约(Contract)。您的接口(interface)缺少 [ServiceContract] 属性。下面将解决您的问题,希望对您有所帮助。

[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string firstName, string lastName);
}

关于c# - 自托管 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15193390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com