Как позвонить в сервис через сервисный канал - PullRequest
0 голосов
/ 28 февраля 2020

Я пытаюсь вызвать сервис через сервисный канал. Мой BeginGet() вызов работает нормально, но EndGet() выдает ошибку приведения, как показано ниже. Может кто-нибудь сказать, что я делаю не так?

Ошибка: невозможно привести объект типа 'Service.AddressServiceClient' к типу 'ServiceClient`1 [IAddressServiceChannel]'.

Мой код:

 public IAsyncResult BeginGet(long id)
 {
        var service = GetClient();  
        IAsyncResult handle = service.BeginGetAddress(id);
        return handle;
 }

 private Service.AddressServiceClient GetClient()
 {
        var client = new Service.AddressServiceClient();
        client.Endpoint.EndpointBehaviors.Clear();
        client.Endpoint.EndpointBehaviors.Add(SetCred()); // setting custom credentials

        return client;
  }

public  EndGet(IAsyncResult handle)
{
     handle.AsyncWaitHandle.WaitOne();

     //ServiceClient is a class which implements IDisposable

     ***// getting runtime exception at following line***
     using (var channel = (ServiceClient<Service.IServiceChannel>)handle.AsyncState) 
     {
            return channel.Client.EndGetAddress(handle)
     }
 }

Я пытался изменить EndGet() следующим образом, но получаю ошибку компиляции:

public  EndGet(IAsyncResult handle)
{
     handle.AsyncWaitHandle.WaitOne();

     ***// get compilation error AddressServiceClient is a type, which is not valid in given context*** 
     using (var channel = <Service.AddressServiceClient> handle.AsyncState)
        {
              return channel.Client.EndGetAddress(handle)
        }
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...