Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自托管WCF服务_C#_Wcf - Fatal编程技术网

C# 自托管WCF服务

C# 自托管WCF服务,c#,wcf,C#,Wcf,我正在考虑从我的应用程序中创建一个自托管WCF服务。我试图在微软网站上仿效这个例子,但我遇到了一个问题 当程序运行时,我可以进入网页,上面说我可以运行svcuti.exe来生成客户机类,或者当我进入WCF测试客户机时,教程说我得到了错误 Service metadata may not be accessible. Make sure your service is running and exposing metadata. 从svcutil.exe中,我得到以下错误: C:\Program

我正在考虑从我的应用程序中创建一个自托管WCF服务。我试图在微软网站上仿效这个例子,但我遇到了一个问题

当程序运行时,我可以进入网页,上面说我可以运行svcuti.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);
            }
        }
公共接口IHelloWorldService
{
[经营合同]
string SayHello(string firstName,string lastName);
}
静态void Main(字符串[]参数)
{
Uri baseAddress=新Uri(“http://localhost:6525/hello");
使用(ServiceHost主机=新ServiceHost(typeof(HelloWorldService),baseAddress))
{
ServiceMetadataBehavior smb=新ServiceMetadataBehavior();
smb.HttpGetEnabled=true;
smb.MetadataExporter.PolicyVersion=PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Open();
WriteLine(“服务在:{0}”处准备就绪,baseAddress);
Console.WriteLine(“按以停止服务”);
Console.ReadLine();
host.Close();
}
}
公共类HelloWorldService:IHelloWorldService
{
公共字符串SayHello(字符串firstName,字符串lastName)
{
返回string.Format(“Hello{0}{1}”,firstName,lastName);
}
}

感谢您提供的任何帮助

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

乍一看,很难找出问题所在,但在测试你的代码时,我发现你的代码中缺少一些东西。你忘了向客户透露你的合同。您的接口缺少[ServiceContract]属性。下面将解决您的问题,希望对您有所帮助

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

此处未公开元数据,您需要为此添加新端点,并需要在服务配置中启用它。请执行以下操作

<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> 


我已经试过了,生成的代理没有任何问题。如果有任何问题,请告诉我。

谢谢,这正是问题所在。工作正常now@Benjiman我没有在公共接口IHelloWorldService定义上方添加
[ServiceContract]