日期:2014-05-20  浏览次数:20726 次

访问wcf程序报错,请高手帮忙解决
有wcf系统,服务器端和客户端均可正常运行,先要做一接口类库,调用客户端程序,调用的客户端程序会涉及wcf服务,现在类库完成以后,.net程序去访问,如果没有wcf的配置文件(App.config),会报错“在 ServiceModel 客户端配置部分中,找不到引用协定“XX”的默认终结点元素。”,在.net程序中增加配置文件(App.config),即可调用成功。但是现在需要用一个非.net其他语言的程序调用,根本没办法使用这个配置文件(App.config),这种情况下怎么办?是不是思路有问题,这种接口该怎么写,谢谢!

------解决方案--------------------
我觉得你还是直接让他们用webservice协议去操作把,这样你过了一手他们未必能用的了了。
------解决方案--------------------
C# code

public static Binding GetBinding(string bindingTypeName, string bindingName)
        {
            IBindingConfigurationElement element = GetBindingConfiguration(bindingTypeName, bindingName);
            return GetBinding(element);
        }

private static Binding GetBinding(IBindingConfigurationElement configurationElement)
        {
            Binding b = null;
            if (configurationElement is CustomBindingElement)
                b = new CustomBinding();
            else if (configurationElement is BasicHttpBindingElement)
                b = new BasicHttpBinding();
            else if (configurationElement is NetMsmqBindingElement)
                b = new NetMsmqBinding();
            else if (configurationElement is NetNamedPipeBindingElement)
                b = new NetNamedPipeBinding();
            else if (configurationElement is NetPeerTcpBindingElement)
                b = new NetPeerTcpBinding();
            else if (configurationElement is NetTcpBindingElement)
                b = new NetTcpBinding();
            else if (configurationElement is WSDualHttpBindingElement)
                b = new WSDualHttpBinding();
            else if (configurationElement is WSHttpBindingElement)
                b = new WSHttpBinding();
            else if (configurationElement is WSFederationHttpBindingElement)
                b = new WSFederationHttpBinding();
            else
                throw new NotSupportedException();
            configurationElement.ApplyConfiguration(b);
            return b;
        }

private static IBindingConfigurationElement GetBindingConfiguration(string bindingTypeName, string bindingName)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(YOUR CONFIGURATION FILE PATH);
            var c = config.GetSectionGroup("system.serviceModel") as ServiceModelSectionGroup;
            BindingCollectionElement element = c.Bindings.BindingCollections.FirstOrDefault(bc =>
            {
                return string.Equals(bc.BindingName, bindingTypeName, StringComparison.OrdinalIgnoreCase);
            });
            if (element == null)
            {
                throw new NotSupportedException();
            }
            return element.ConfiguredBindings.FirstOrDefault(b =>
            {
                return string.Equals(b.Name, bindingName, StringComparison.OrdinalIgnoreCase);
            });
        }