WCF服务中的事件/回调

时间:2014-11-19 21:39:23

标签: wcf events service callback

嗨,我有一个Windows窗体谁有一个wcf服务,我想在我输入时使用来自url的数据更改该格式的标签。 IWCFService.cs和WCFService.cs为空。我不知道该写些什么。我不希望客户端使用Windows窗体。我想在浏览器中点击网址时从网址中捕获数据。没有更多的客户形式或其他东西。

这是我的代码:

Ταμείο.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;

namespace Ταμείο
{
    public partial class Form1 : Form
    {
        ServiceHost host;
        string WCFPort;

        private readonly WCFService _WcfData;

        public Form1()
        {
            InitializeComponent();
            WCFPort = "8001";
            StartWCFServer();

            _WcfData = new WCFService();
            _WcfData.getData += new EventHandler<GetDataEventHandler>(_WcfData_getData);
        }

        void _WcfData_getData(object sender, GetDataEventHandler e)
        {
            MessageBox.Show(e.Data);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void StartWCFServer()
        {
            if (host == null)
            {
                Uri baseAddress = new Uri("http://localhost:" + WCFPort + "/");
                host = new ServiceHost(typeof(WCFService), baseAddress);
                host.AddServiceEndpoint(typeof(IWCFService), new WSHttpBinding(), "Services");
                try { host.Open(); }
                catch { MessageBox.Show("Βεβαιωθείτε ότι έχετε δικαιώματα διαχειριστή σε αυτόν τον υπολογιστή"); }
            }
            else
            {
                MessageBox.Show("Υπήρξε πρόβλημα κατά του άνοιγμα του WCF Server. Είτε ο WCF Server είναι Ενεργός, είτε το Port: " + WCFPort + " χρεισιμοποιείτε κάπου αλλού, είτε η IP του δικτύου δεν είναι σωστή");
            }
        }
    }
}

的App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Ταμείο.WCFService" behaviorConfiguration="ServiceBehaviour">
        <endpoint bindingConfiguration="webHttpBindingDev" address="" binding="webHttpBinding" contract="Ταμείο.IWCFService" behaviorConfiguration="web"></endpoint>

      </service>
    </services>
    <behaviors>
      <serviceBehaviors>

        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <serviceDebug httpsHelpPageEnabled ="true" httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"  />
    </protocolMapping>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingDev">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"  maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None" />

        </binding>
      </webHttpBinding>

    </bindings>

  </system.serviceModel>
  <system.webServer>

    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

IWCFService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

namespace Ταμείο
{
    [ServiceContract]
    public interface IWCFService
    {
        [OperationContract(Name = "getData")]
    [WebGet(UriTemplate="get?Data={data}")]
        void get(string Data);
    }
}

WCFSerivice.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Ταμείο
{
    public class WCFService : IWCFService
    {
        public event EventHandler<GetDataEventHandler> getData = delegate { };

        public void get(string message)
        {
            OnDataFound(new GetDataEventHandler(message));
        }

        protected virtual void OnDataFound(GetDataEventHandler e)
        {
            getData(this, e);
        }
    }

    public class GetDataEventHandler : EventArgs
    {
        public string Data { get; private set; }

        public GetDataEventHandler(string data)
        {
            Data = data;
        }
    }
}

0 个答案:

没有答案