加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何使用Delphi 2007中的非IIS托管,WCF,C#Web服务?

发布时间:2020-12-15 10:12:27 所属栏目:大数据 来源:网络整理
导读:我编写了一个相当简单的小型C#Web服务,通??过WCF从独立的EXE托管.代码 – 有点简化 – 看起来像这样: namespace VMProvisionEXE{class EXEWrapper{ static void Main(string[] args) { WSHttpBinding myBinding = new WSHttpBinding(); myBinding.Security.
我编写了一个相当简单的小型C#Web服务,通??过WCF从独立的EXE托管.代码 – 有点简化 – 看起来像这样:
namespace VMProvisionEXE
{
class EXEWrapper
{
    static void Main(string[] args)
    {
        WSHttpBinding myBinding = new WSHttpBinding();
        myBinding.Security.Mode = SecurityMode.None;

        Uri baseAddress = new Uri("http://bernard3:8000/VMWareProvisioning/Service");
        ServiceHost selfHost = new ServiceHost(typeof(VMPService),baseAddress);

        try
        {
            selfHost.AddServiceEndpoint(typeof(IVMProvisionCore),myBinding,"CoreServices");

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy12;
            selfHost.Description.Behaviors.Add(smb);

            // Add MEX endpoint
            selfHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,MetadataExchangeBindings.CreateMexHttpBinding(),"mex");

            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.ReadLine();

其余的C#代码;上面的类VMPService实现了VMProvisionCore.IVMProvisionCore.

namespace VMProvisionCore
{
[ServiceContract(Namespace = "http://Cisco.VMProvision.Core",ProtectionLevel = System.Net.Security.ProtectionLevel.None)]
public interface IVMProvisionCore
{
    [OperationContract]
    bool AuthenticateUser(string username,string password);
}

我可以轻松创建使用此服务的Visual Studio 2008客户端应用程序.没问题.但是使用Delphi 2007是一个不同的问题.我可以使用Delphi中的WSDL导入器从(在这种情况下)检索WSDL http://bernard3:8000/VMWareProvisioning/Service?wsdl导入单元编译得很好.我必须手动初始化代理,因为WSDL不包含URL(注意C#代码中显示的额外“/ CoreServices”):

var
  Auth: AuthenticateUser;
  AuthResponse: AuthenticateUserResponse;
  CoreI: IVMProvisionCore;
begin
  CoreI:= GetIVMProvisionCore(False,'http://bernard3:8000/VMWareProvisioning/Service/CoreServices');
  Auth:= AuthenticateUser.Create;
  try
    Auth.username:= 'test';
    Auth.password:= 'test';
    AuthResponse:= CoreI.AuthenticateUser(Auth);
  finally
    FreeAndNIL(Auth);
  end;

上面的代码在遇到“CoreI.AuthenticateUser(Auth);”时会产生错误.错误是“无法处理消息,因为内容类型’text / xml; charset =”utf-8“不是预期类型’application / soap xml; charset = utf-8.”

我怀疑我在某处有一个愚蠢的小错误,可能是在导入WSDL或连接选项之类的时候.有人可以帮忙吗?

解决方法

找到了解决方案.它是多个部分,需要对C#端进行一些更改,对Delphi端更多.请注意,这是使用Delphi 2007和Visual Studio 2008测试的.

C#方:
使用BasicHttpBinding而不是WSHttpBinding.

修复第1步

BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.Security.Mode = BasicHttpSecurityMode.None;

此更改将解决Delphi端的application / soap xml错误.

Delphi 2007方面:
针对修改后的C#Web服务运行现在将生成如下错误:

Exception class ERemotableException
with message ‘The message with Action
” cannot be processed at the
receiver,due to a ContractFilter
mismatch at the EndpointDispatcher.
This may be because of either a
contract mismatch (mismatched Actions
between sender and receiver) or a
binding/security mismatch between the
sender and the receiver. Check that
sender and receiver have the same
contract and the same binding
(including security requirements,e.g.
Message,Transport,None).’

要解决此问题,请将SOAPActions添加到所有支持的接口.这是我的代码中的示例;这必须在import-from-WSDL-PAS-file的初始化部分所做的所有InvRegistry更改之后完成:

修复第2步

InvRegistry.RegisterDefaultSOAPAction(TypeInfo(IVMProvisionCore),'http://Cisco.VMProvision.Core/CoreServices/%operationName%');

类型名称和URL应该可以从Delphi生成的WSDL导入文件和/或实际WSDL的检查中获得.上面的例子是我自己的项目.这些代码更改后,您将出错:

Exception class ERemotableException
with message ‘The formatter threw an
exception while trying to deserialize
the message: Error in deserializing
body of request message for
operation….

通过添加以下代码(信用到http://www.bobswart.nl/weblog/Blog.aspx?RootId=5:798)解决此错误.同样,这个新代码必须在WSDL-to-PAS文件初始化之后的所有InvRegistry之后.

修复第3步

InvRegistry.RegisterInvokeOptions(TypeInfo(IVMProvisionCore),ioDocument);

此时,数据包将在Delphi和C#之间来回传递 – 但参数将无法正常工作. C#将接收所有参数作为空值,Delphi似乎没有正确接收响应参数.最后的代码步骤是使用稍微定制的THTTPRIO对象,该对象将允许文字参数.这部分的技巧是确保在获得接口后应用该选项;之前这样做是行不通的.这是我的例子中的代码(只是片段).

修复第4步

var
  R: THTTPRIO;
  C: IVMProvisionCore;
begin
  R:= THTTPRIO.Create(NIL);
  C:= GetIVMProvisionCore(False,TheURL,R);
  R.Converter.Options:= R.Converter.Options + [soLiteralParams];

现在 – 我的Delphi 2007应用程序可以与C#,独立,非IIS,WCF Web服务进行通信!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读