自托管WCF服务

本文关键字:服务 WCF | 更新日期: 2023-09-27 18:02:06

我正在考虑在我的应用程序中创建一个自托管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);
            }
        }

感谢您为提供的任何帮助

自托管WCF服务

乍一看,很难弄清楚问题的问题,但当我测试您的代码时,我发现您的代码中缺少一些东西。你忘了向客户公开你的合同。您的接口缺少[ServiceContract]属性。下面将解决您的问题,希望这能有所帮助。

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

我刚刚遇到了完全相同的问题。事实证明,在编辑了app.config之后,设计器停止了对app.config文件的自动更新。你能发布app.config的标记吗?

元数据没有在这里公开,您需要为此添加新的EndPoint,并需要在服务配置中启用它。请执行以下操作。

<service name="ConsoleApplication1.WCFService1" behaviorConfiguration="newBehaviour" >
        <endpoint address="mex"  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
        <host>
            <baseAddresses>
                <add baseAddress="http://localhost:80/WCFService1" />
            </baseAddresses>
        </host>
<service>

以及在服务行为中

<serviceBehaviors>
          <behavior name="newBehaviour">
            <serviceMetadata httpGetEnabled="true"/>
          </behavior>
</serviceBehaviors> 

我已经尝试过了,并在没有任何问题的情况下生成了代理。如果有任何顾虑,请告诉我。