【问题标题】:Learning WCF - Visual Studio IDE学习 WCF - Visual Studio IDE
【发布时间】:2014-03-31 07:25:52
【问题描述】:

我是 WCF 新手,目前正在使用“学习 WCF:动手指南”一书。这本书使用 VS2008 作为示例,我不确定使用什么 Visual Studio IDE 作为示例。我尝试使用 VS Express for Web 并给出以下错误:

“HelloIndigo.exe 不包含适用于入口点的静态 Main 方法”。

我可以理解问题的原因,但我不确定在哪里添加 main 方法。所以我使用了桌面版 VS Express,它工作得很好,但是当我在第一章中继续进行时,我无法继续,因为桌面版 VS Express 中没有 WCF 服务模板。 VS2012 仅提供免费试用版,有效期为 90 天。那么我应该使用什么IDE?如果答案是 VS Express for Web,那么如何修复第一章示例的错误? 书中提供的例子是 主持人:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace Host
{
class Program
{
    static void Main(string[] args)
    {
        using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService),new Uri("http://localhost:8000/HelloIndigo")))
        {
            host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new BasicHttpBinding(), "HelloIndigoService");
            host.Open();

            Console.WriteLine("Please <ENTER> to terminate the service host");
            Console.ReadLine();
        }
    }
 }
}

你好靛蓝:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace HelloIndigo
{
 public class HelloIndigoService : IHelloIndigoService
 {
    public string HelloIndigo()
    {
        return "Hello Indigo";
    }
}
[ServiceContract(Namespace="http://www.thatindigogirl.com/samples/2006/06")]
public interface IHelloIndigoService
{
    [OperationContract]
    string HelloIndigo();
}

}

客户: 程序.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace Client
{
class Program
{
    static void Main(string[] args)
    {
        EndpointAddress ep = new EndpointAddress("http://localhost:8000/HelloIndigo/HelloIndigoService");
        IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.CreateChannel(new BasicHttpBinding(), ep);
        string s = proxy.HelloIndigo();
        Console.WriteLine(s);
        Console.WriteLine("Please <ENTER> to terminate client");
        Console.ReadLine();
    }
}
}

ServiceProxy.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace Client
{
class ServiceProxy
{
}
[ServiceContract(Namespace = "http://www.thatindigogirl.com/samples/2006/06")]
public interface IHelloIndigoService
{
    [OperationContract]
    string HelloIndigo();
}

}

【问题讨论】:

  • 你能贴一些代码让我们看看吗?所有程序都需要一个 main 方法。因此,如果您没有,那么您将无法调试您正在编写的应用程序,否则它无法自行启动。

标签: c# wcf visual-studio visual-studio-2012


【解决方案1】:

HelloIndigo 应编译为库 (DLL) 而不是可执行文件。所以不应该有Main 方法——它没有一个作为类库的方法。

Host 的意义在于它将托管服务库 HelloIndigo 并开始侦听端点上针对该特定服务的调用。

更改HelloIndigo 以编译为类库并在Host 中添加对HelloIndigo 的引用。然后启动Host 进程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多