首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在同一解决方案中添加WCF引用,而不添加服务引用

在同一解决方案中添加WCF引用,而不添加服务引用
EN

Stack Overflow用户
提问于 2012-08-22 16:36:18
回答 1查看 975关注 0票数 2

我正在做一个使用windows Azure的项目,在worker角色中,我想使用webservice向it.My提交一些信息,问题是:我可以在不添加服务引用的情况下使用webservice吗?或者以某种方式添加,当我在azure中发布我的项目时,我不需要更改服务引用?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-22 17:18:40

您可以使用通道工厂直接连接。下面是一个示例存储库基类,您可以覆盖它,其中T是您的服务契约,例如IMyService,

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace pageengine.clients
{
    public class RepositoryBase<T> : IDisposable
    {

        #region Channel
        protected String roleName;
        protected String serviceName;
        protected String endpointName;
        protected String protocol = @"http";

        protected EndpointAddress _endpointAddress;
        protected BasicHttpBinding httpBinding;
        protected NetTcpBinding tcpBinding;

        protected IChannelFactory channelFactory;
        protected T client;

        protected virtual AddressHeader[] addressHeaders
        {
            get
            {
                return null;
            }
        }

        protected virtual EndpointAddress endpointAddress
        {
            get
            {
                if (_endpointAddress == null)
                {
                    var endpoints = RoleEnvironment.Roles[roleName].Instances.Select(i => i.InstanceEndpoints[endpointName]).ToArray();
                    var endpointIP = endpoints.FirstOrDefault().IPEndpoint;
                    if(addressHeaders != null)
                    {
                        _endpointAddress = new EndpointAddress(new Uri(String.Format("{1}://{0}/{2}", endpointIP, protocol, serviceName)), addressHeaders);
                    }
                    else
                    {
                        _endpointAddress = new EndpointAddress(String.Format("{1}://{0}/{2}", endpointIP, protocol, serviceName));
                    }

                }
                return _endpointAddress;
            }
        }

        protected virtual Binding binding
        {
            get
            {
                switch (protocol)
                {
                    case "tcp.ip":
                        if (tcpBinding == null) tcpBinding = new NetTcpBinding();
                        return tcpBinding;
                    default:
                        //http
                        if (httpBinding == null) httpBinding = new BasicHttpBinding();
                        return httpBinding;
                }
            }
        }

        public virtual T Client
        {
            get
            {
                if (this.client == null)
                {
                    this.channelFactory = new ChannelFactory<T>(binding, endpointAddress);
                    this.client = ((ChannelFactory<T>)channelFactory).CreateChannel();
                    ((IContextChannel)client).OperationTimeout = TimeSpan.FromMinutes(2);
                    var scope = new OperationContextScope(((IContextChannel)client));
                    addCustomMessageHeaders(scope);
                }
                return this.client; 
            }
        }

        protected virtual void addCustomMessageHeaders(OperationContextScope operationContextScope)
        {
            // Overidden
        }
        #endregion

        #region CTOR
        public RepositoryBase()
        {

        }
        #endregion

        #region IDisposable Members

        public virtual void Dispose()
        {
            if (client != null)
            {
                try
                {
                    ((ICommunicationObject)client).Close();
                }
                catch
                {
                    try
                    {
                        ((ICommunicationObject)client).Abort();
                    }
                    catch { } // Die quietly.
                }
            }
            if (channelFactory != null)
            {
                try
                {
                    channelFactory.Close();
                }
                catch
                {
                    try
                    {
                        channelFactory.Abort();
                    }
                    catch { } // Die quietly.
                }
                channelFactory = null;
            }
            _endpointAddress = null;
            httpBinding = null;
            tcpBinding = null;
        }

        #endregion
    }
}

然后,您将发现端点,它将在发布和模拟环境中工作。扩展此基础的类可能如下所示:

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

using pageengine.services.entities.account;
using pageengine.services;

namespace pageengine.clients.accounts
{
    public class AccountsRepository : RepositoryBase<IAccounts>, IDisposable
    {
        #region CTOR
        public AccountsRepository()
        {
            this.roleName = "EntitiesRole";      // Name of the role my service is on
            this.endpointName = "HttpInternal";  // Name of the endpoint configured on that role. Can be internal or input, tcp or http.
            this.serviceName = "Accounts.svc";   // Name of my service.
        }
        #endregion
    }
}

对您的服务的调用(在本例中是从MVC控制器操作)将采用以下形式:

代码语言:javascript
运行
复制
    public ActionResult ListAccounts()
    {
        using (var accountsRepository = new AccountsRepository())
        {
            return View("ListAccounts", accountsRepository.Client.ListAccounts());
        }
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12069240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档