【问题标题】:How do I solve the error in my WCF service and proceed on?如何解决我的 WCF 服务中的错误并继续?
【发布时间】:2011-11-12 10:10:51
【问题描述】:

我目前正在开发一个 C# Windows 窗体应用程序,我打算让它与服务器交互。服务器将接收来自我开发的移动应用程序的帖子,每当收到帖子时,我的 Windows 窗体应用程序都会收到通知并给我一个通知。

例如我的移动应用程序向我的服务器发送了一条消息。一旦我的服务器收到消息,我的 Windows 窗体应用程序应该会显示一条新通知,显示收到的消息内容。

我现在开始开发我的 WCF 服务,这就是我目前所做的

[ServiceContract(Namespace = "Posting")]
public interface IPostingService
{
    [OperationContract]
    void NotifyAboutPosting(Posting post);
}

[DataContract]
public class Posting
{
    [DataMember]
    public int ID { get; set; }
    [DataMember]
    public string Description { get; set; }
    [DataMember]
    public DateTime PostingTimestamp { get; set; }
}

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

        ServiceHost selfHost = new ServiceHost(typeof(Posting), baseAddress);

        try
        {

            // Step 3 of the hosting procedure: Add a service endpoint.
            selfHost.AddServiceEndpoint(
                typeof(IPostingService),
                new WSHttpBinding(),
                "Posting");


            // Step 4 of the hosting procedure: Enable metadata exchange.
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            selfHost.Description.Behaviors.Add(smb);

            // Step 5 of the hosting procedure: Start (and then stop) the service.
            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown the service.
            selfHost.Close();
        }
        catch (CommunicationException ce)
        {
            Console.WriteLine("An exception occurred: {0}", ce.Message);
            selfHost.Abort();
        }

    }
}

关于发帖类,我想问的是里面有哪些方法是用来从服务器获取信息的?

服务完成后如何从这里继续。 (我的 winform 应用程序已经完成,剩下的就是添加这个逻辑,以便在移动应用程序发送到服务器时接收发布。

似乎有一个编译错误

在服务“##.Posting”实施的合同列表中找不到合同名称“##.IPostingService”。

谁能帮我解决这个问题?谢谢一百万!

【问题讨论】:

    标签: wcf


    【解决方案1】:

    您的实际实施在哪里?您有合同 (IPostingService)、数据 (Posting)...但是代码在哪里进行工作?您似乎缺乏合同执行:

    public class PostingService : IPostingService
    {
        public void NotifyAboutPosting(Posting post)
        {
            // do something with post here
        }
    }
    

    并且您在设置主机时注册了实际的工人阶级(不是数据):

    ServiceHost selfHost = new ServiceHost(typeof(PostingService), baseAddress);
    

    【讨论】:

    • 嗨。谢谢您的回复。所以就我而言,我将在该课程中执行该服务的目的是什么?在这种情况下,哪个检查来自服务器的新帖子?
    • @Thomas:是的,在那个类中你放置了 service 实现。然而,Web 服务并不是最适合通知(如在观察者-订阅者模式中),将它们视为获取数据的一种手段。因此,您的 winforms 应用程序必须定期查询 Web 服务(“向我发送所有新消息” - 可以这样做),或者您需要更改模型。
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多