用户登录
用户注册

分享至

WCF代码实现同等级的App.config配置

  • 作者: 嗫?暁雲?
  • 来源: 51数据库
  • 2021-09-21

WCF代码实现同等级的App.config配置@TOC

二者使用缺陷

   之前自己在做WCF的时候就是简单的本地服务调用,而且由于自己是新手,那么就使用常规操作进行配置,就是一般的配置文件配置的方式进行。这个之前已有说过,不懂得可以参阅之前的博客。
   但是,当其他程序调用你的服务,或者是不确定的多个程序,或者是不确定的语言,或者是不确定的客户端还是服务端,该如何处置。
   本地 配置易于初始学习,了解WCF,但是如果其他程序调用的话,需要把对应的配置文件的system.serviceModel提供给宿主程序,才可以进行调用。这样其实是很不方便的。
   因为,在需求和高效的性能下,把配置写在主程序 中代码里,而不是配置文件就能很好的解决这个问题,当程序运行,代码加载就自动执行了对应的配置信息

新的改变,后端代码附上

自己专研,调试一下午的收获:
public CAServiceForm()
{
InitializeComponent();
Thread lanECG = new Thread(StartECGService);
lanECG.IsBackground = true;
lanECG.Start();
}
private void StartECGService()
{
try
{
string ECGServiceUrl = CommConfigHelper.CAServerIPInfo;//为了不与IIS部署的web应用重名,故用MedExECGCommunicationWCFService服务名(CommConfigHelper.CAServerIPInfo为配置文件输入的控制IP地址和服务地址,如"http://{0}/MedExCAService")

            Uri address = new Uri(ECGServiceUrl);
            m_WECGServiceHost = new ServiceHost(typeof(ServiceInterface.CAService), address);//实例化WCF服务对象

            ServiceMetadataBehavior behaviour = m_WECGServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (behaviour == null)
             behaviour = new ServiceMetadataBehavior();
            //设置允许进行HttpGet操作。  
            behaviour.HttpGetEnabled = true;
            //设置MetadataExporter导出Metadata时遵循WS-Policy 1.5规范。  
            behaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

            //将创建好的behaviour加入到宿主实例的行为描述组当中。  
            m_WECGServiceHost.Description.Behaviors.Add(behaviour);

            m_WECGServiceHost.AddServiceEndpoint(
            ServiceMetadataBehavior.MexContractName,
            MetadataExchangeBindings.CreateMexHttpBinding(),
            "mex"
            );
            WSHttpBinding wsBinding = new WSHttpBinding();
            BasicHttpBinding wsBinding = new BasicHttpBinding();
            wsBinding.MaxBufferPoolSize = int.MaxValue;
            wsBinding.MaxBufferSize = int.MaxValue;
            wsBinding.MaxReceivedMessageSize = int.MaxValue;
            wsBinding.ReaderQuotas = new XmlDictionaryReaderQuotas()
            {

                MaxDepth = 64,
                MaxArrayLength = int.MaxValue,
                MaxBytesPerRead = int.MaxValue,
                MaxNameTableCharCount = int.MaxValue,
                MaxStringContentLength = int.MaxValue
            };
            //启动CA服务
            m_WECGServiceHost.AddServiceEndpoint(typeof(Interface.ICAService),//契约(C)
                                               wsBinding,            //绑定(B)
                                               ECGServiceUrl); //地址(A)
            m_WECGServiceHost.Open();
            ShowMsg(string.Format("CA服务启动成功:{0}", address));
        }
        catch (Exception ex)
        {
            string msg = string.Format("{0}服务启动异常:{1}", CommConfigHelper.ReadAppSettingsString("CAServerIPInfo"), ex.Message);
            ShowMsg(msg);
            LogHelper.Write(LogLevel.ERROR, "服务启动异常", msg);
            MessageBox.Show(msg);
        }

    }

js调用代码

收获

 在写代码的时候,初始的时候可以为了学习,但是慢慢的熟悉了,不能安于现状,要想着如果有更高的需求,那我目前的代码是否可以解决该问题,不能的话,该怎么优化,怎么实现。而且 当自己在写服务的时候,一定想着是多种语言,多种平台的无缝对接,就是对方直接调用,而不是在进行修改配置什么,除了参数!!!
软件
前端设计
程序设计
Java相关