سؤال

أواجه بعض المشكلات في الحصول على عمليات استدعاء تعمل مع عميل/خادم WCF Nettcpbinding الخاص بي. هنا هو الكود ... أي أفكار؟

جانب الخدمة

عقد:

using System.Runtime.Serialization;
using System.ServiceModel;

namespace API.Interface
{
   [ServiceContract(CallbackContract = typeof(IServiceCallback))]
   public interface IMyService
   {
       [OperationContract]
       void DoSomething();
   }

   public interface IServiceCallback
   {
        [OperationContract(IsOneWay = true)]
        void OnCallback();
   }
}

الخدمات:

using System;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.Timers;
using API.Interface;

namespace API.Service
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
    public class MyService : IMyService
    {
        public static IServiceCallback callback;
        public static Timer Timer;

        public void DoSomething()
        {
            Console.WriteLine("> Session opened at {0}", DateTime.Now);
            callback = OperationContext.Current.GetCallbackChannel<IServiceCallback>();

            Timer = new Timer(1000);
            Timer.Elapsed += OnTimerElapsed;
            Timer.Enabled = true;

        }

        void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            callback.OnCallback();
        }
    }
}

إليك الرمز الذي أستخدمه لبدء الخدمة

        var service = new MyService();
        // Start up the WCF API
        var service = new ServiceHost(turboService);
        service.Open();

ها هو app.config

   <system.serviceModel>
    <services>
      <service name="API.Service.MyService">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="API.Interface.IMyService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8732/MyService/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

جانب العميل

خدمة التراجع

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using USBAutomationTester.ServiceReference;

namespace USBAutomationTester
{
    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false)]
    public class CallbackService : IMyServiceCallback
    {
        public void OnCallback()
        {
            Console.WriteLine("> Received callback at {0}", DateTime.Now);
        }
    }
}

الاتصال والاتصال

 var instanceContext = new InstanceContext(new CallbackService());
 var service = new TurboValidateServiceClient(instanceContext);
 service.DoSomething();

app.config

  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_IMyService" />
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint address="net.tcp://localhost:8732/MyService/"
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ITurboValidateService"
        contract="ServiceReference.IMyService"
        name="NetTcpBinding_IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

أعتقد أن لدي كل القطع اللازمة. قادني البحث في Google إلى أسفل عدة مسارات مختلفة دون أي نتيجة حقيقية. أستطيع أن أرى الخدمة التي تسمي رد الاتصال ، لكن موكلي لا يحصل عليها أبدًا.

شكرًا مقدمًا ، أعرف أن هذا سؤال من نوع WCF 101 ، لكنني متعثر في هذه المرحلة.

تحديث

على العميل ، أحصل على هذا الاستثناء

"لا يمكن معالجة الرسالة الواردة مع الإجراء لأنها تستهدف عملية تكرار الطلب ، ولكن لا يمكن الرد عليها لأن خاصية MessageId لم يتم تعيينها."

تليها

"تلقت القناة رسالة إدخال غير متوقعة مع الإجراء"http://tempuri.org/imyservice/oncallbackأثناء الإغلاق. يجب عليك إغلاق قناتك فقط عندما لا تتوقع أي رسائل إدخال أخرى. "

هل كانت مفيدة؟

المحلول

قد تكون المشكلة هي أن السياق مغلق بالفعل بحلول الوقت الذي يحرش فيه المؤقت.

    public void DoSomething()
    {
        Console.WriteLine("> Session opened at {0}", DateTime.Now);
        callback = OperationContext.Current.GetCallbackChannel<IServiceCallback>();

        Timer = new Timer(1000);
        Timer.Elapsed += OnTimerElapsed;
        Timer.Enabled = true;

    }

    void OnTimerElapsed(object sender, ElapsedEventArgs e)
    {
        callback.OnCallback();
    }

حاول إلقاء ردك على ICommunicationObject والتحقق من State منشأه. إذا لم يتم ضبطه على Open عندما تحاول OnCallback, ، هذه مشكلتك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top