كيفية فضح واجهات عقد الخدمة مع ميراث متعددة في خدمة WCF على نقطة نهاية واحدة

StackOverflow https://stackoverflow.com/questions/2500336

  •  21-09-2019
  •  | 
  •  

سؤال

ليس لدي سوى أنواع بيانات بسيطة في توقيع الأسلوب للخدمة (مثل int ، string). يقول فئة الخدمة الخاصة بي واجهة ServiceContract SENESS IMATHSERVICE ، وهذه الواجهة بدورها ترث من بعض الواجهة الأساسية الأخرى تقول IADDERSERVICE. أرغب في فضح خدمة الرياضيات باستخدام عقد الواجهة IadDerservice كخدمة على نقطة نهاية واحدة. ومع ذلك ، يجب أن تكون بعض من Clinet التي تعرف عن Imathservice قادرة على الوصول إلى الخدمات الإضافية التي تقدمها ImathService على نقطة النهاية الفردية IE عن طريق مجرد كتابة iadderservice إلى Imathservice.

//Interfaces and classes at server side
[ServiceContract]
public interface IAdderService
{
[OperationContract]
int Add(int num1, int num2);
}

[ServiceContract]
public interface IMathService : IAdderService
{
[OperationContract]
int Substract(int num1, int num2);
}

public class MathService : IMathService
{
#region IMathService Members
public int Substract(int num1, int num2)
{
    return num1 - num2;
}
#endregion

#region IAdderService Members
public int Add(int num1, int num2)
{
    return num1 + num2;
}
#endregion
}

//Run WCF service as a singleton instace
MathService mathService = new MathService();
ServiceHost host = new ServiceHost(mathService);
host.Open();

تكوين جانب الخادم:

 <configuration>
   <system.serviceModel>
     <services>
            <service name="IAdderService"
         behaviorConfiguration="AdderServiceServiceBehavior"> 
        <endpoint address="net.pipe://localhost/AdderService"
      binding="netNamedPipeBinding"
      bindingConfiguration="Binding1"
      contract="TestApp.IAdderService" />
         <endpoint address="mex"
      binding="mexNamedPipeBinding"
      contract="IMetadataExchange" />
        <host>
           <baseAddresses>
            <add baseAddress="net.pipe://localhost/AdderService"/>
           </baseAddresses>
        </host>
           </service>
          </services>
       <bindings>
        <netNamedPipeBinding>
      <binding name="Binding1" >
          <security mode = "None">
           </security>
           </binding >
        </netNamedPipeBinding>
       </bindings>

      <behaviors>
        <serviceBehaviors>
     <behavior name="AdderServiceServiceBehavior">
            <serviceMetadata />
       <serviceDebug includeExceptionDetailInFaults="True" />
     </behavior>
        </serviceBehaviors>
      </behaviors>
     </system.serviceModel>
    </configuration>

imeplementation جانب العميل:

IAdderService adderService = new
ChannelFactory<IAdderService>("AdderService").CreateChannel();
int result = adderService.Add(10, 11);

IMathService mathService = adderService as IMathService; 
result = mathService.Substract(100, 9);

تكوين جانب العميل:

    <configuration>
       <system.serviceModel>
          <client>
            <endpoint name="AdderService" address="net.pipe://localhost/AdderService" binding="netNamedPipeBinding" bindingConfiguration="Binding1" contract="TestApp.IAdderService" />
          </client>

          <bindings>
            <netNamedPipeBinding>
         <binding name="Binding1" maxBufferSize="65536" maxConnections="10">
          <security mode = "None">
          </security>
         </binding >
            </netNamedPipeBinding>
          </bindings>
        </system.serviceModel>
      </configuration>

باستخدام الكود أعلاه والتكوين ، لا أستطيع أن أتمكن من typecast iadderservice instnace إلى imathservice ، فهو يفشل وأحصل على مثيل لاغاني من الخدمات في جانب العميل.

ملاحظتي هي أنه إذا قام الخادم بتكليف ImathService بالعميل ، فسيكون العميل بأمان أن يكون ذلك بأمان إلى iadderservice والعكس صحيح ممكن أيضًا. ومع ذلك ، إذا قام الخادم بفضح iAdDerservice ، فإن typecast يفشل.

هل هناك أي حل لهذا؟ أو هل أفعل ذلك بطريقة خاطئة.

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

المحلول

تمامًا كما لاحظت ، عليك فضح الخدمة المشتقة (خدمة الرياضيات) بدلاً من الخدمات المدمجة.

ليس لدى WCF أي فكرة أن الخدمات المدمرة تحدث في الواقع خدمة رياضيات تنفذ واجهة لخدمة الرياضيات. يعتبر الخدمات المدمجة - لذلك لا توجد طريقة لإلقاءها على فئة مشتقة.

نصائح أخرى

قمت بتكرار وضعك بالضبط إلا أنني استخدمت IMathService نقطة النهاية بدلاً من IAdderService نقطة النهاية.

IAdderService adderService = new ChannelFactory<IMathService>("MathService").CreateChannel();
int result = adderService.Add(10, 11);

IMathService mathService = adderService as IMathService;
result = mathService.Substract(100, 9);

هذا يعمل بالنسبة لي. لا يمكنك إلقاء نوع قاعدة لنوع مشتق. يمكنك القيام بالعكس.

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