0

我的目标是能够从使用基于 SOAP 的 API 的端点发布和检索


我的项目结构

我生成了一个带有 WSDL 文件的客户端以定位 cucm 11.5,然后

我按照github上的示例创建了所有在 repo 上完成的类和接口

第三,我的解决方案由两个项目组成,一个类库和一个控制台项目,类库包含从 WSDL 文件生成的客户端,控制台项目由与类库项目交互的类和接口组成


我有以下课程来执行操作


 public class TestAxl
{

    public void CreateUsers()
    {
        var axlClient = new AxlClient(new AxlClientConfiguration
        {
            Server = "Ip to the publish server",

            User = "administrator",
            Password = "password provided"


        });




        var addUserResult = axlClient.ExecuteAsync(async client =>
       {
           var userId = Guid.NewGuid().ToString();
           var request = new AddUserReq
           {
               user = new XUser
               {
                   userid = userId,
                   userIdentity = userId,
                   password = "P@ssw0rd",
                   firstName = "test",
                   lastName = "test"
               }
           };
           var response = await client.addUserAsync(request);
           return response.addUserResponse1.@return;
       });



    }



}

我像这样从主类中调用它


 class Program
{
    static void Main(string[] args)
    {



        var letsDoSomeTesting = new TestAxl();

        try
        {
             letsDoSomeTesting.CreateUsers();



        }
        catch (Exception e)
        {



            Console.WriteLine("The following is the exceeption from calling final class ", e.Message);

        }





    }



}

当我尝试运行控制台项目时,它以 0 启动并退出,

然后我回到 CUCM 沙箱环境,没有任何改变,可能是什么原因导致此操作无法正常工作

仅供参考:运行时 netCore 3.1

4

1 回答 1

1

我能够在 Linux 上使用 DotNet Core 3.1 获得一个示例项目,包括 AXL/addUser:https ://github.com/CiscoDevNet/axl-dotnet-samples

这是主要部分:

// Create a custom binding so we can allow the client to use cookies with AXL
BasicHttpsBinding binding = new BasicHttpsBinding();
binding.AllowCookies = true;

// Specify the CUCM AXL API location for the SOAP client
EndpointAddress address = new EndpointAddress( $"https://{ System.Environment.GetEnvironmentVariable( "CUCM_ADDRESS" ) }:8443/axl/" );

//Class generated from AXL WSDL
AXLPortClient client = new AXLPortClient( binding, address );

// To disable HTTPS certificate checking, uncomment the below lines
// NOT for production use!

// client.ChannelFactory.Credentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication
//     {
//         CertificateValidationMode = X509CertificateValidationMode.None,
//         RevocationMode = X509RevocationMode.NoCheck
//     };
// client.ChannelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
// client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;

// Incantation to force alternate serializer reflection behaviour due to complexities in the AXL schema
// See https://github.com/dotnet/wcf/issues/2219
MethodInfo method = typeof( XmlSerializer ).GetMethod( "set_Mode", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static );
method.Invoke( null, new object[] { 1 } ); 

// Base64 encode AXL username/password for Basic Auth
var encodedUserPass = Convert.ToBase64String( Encoding.ASCII.GetBytes( 
    System.Environment.GetEnvironmentVariable( "CUCM_USERNAME" ) + ":" +
    System.Environment.GetEnvironmentVariable( "CUCM_PASSWORD" )
) );

// Incantation to create and populate a Basic Auth HTTP header
// This must be done to force SoapCore to include the Authorization header on the first attempt
// rather than in challenge/response fashion
HttpRequestMessageProperty requestProperty = new HttpRequestMessageProperty();
requestProperty.Headers[ "Authorization" ] = "Basic " + encodedUserPass;

// Creating context block apparently allows attaching custom HTTP headers to the request
var scope = new OperationContextScope( client.InnerChannel );
OperationContext.Current.OutgoingMessageProperties[ HttpRequestMessageProperty.Name ] = requestProperty;

//Create the request object
AddUserReq addUserReq = new AddUserReq(); 

addUserReq.user = new XUser();
addUserReq.user.lastName = "TestUser";
addUserReq.user.userid = "testUser";
addUserReq.user.password = "Cisco!1234";

string userPkid = "";

//Try the addUser request
try
    {
        addUserResponse addUserResp = await client.addUserAsync( addUserReq );
        userPkid = addUserResp.addUserResponse1.@return;
    }
catch ( Exception ex )
    {
        Console.WriteLine( $"\nError: addUser: { ex.Message }" );
        Environment.Exit( -1 );
    }

几点注意事项:

  • SoapCore 会在可能的情况下生成具有默认值的元素,例如字符串元素的 nil。这会导致问题<addUser>,因为<customerName>元素应该只发送到HCS CUCM。在运行 svcutil 之前对 AXLSoap.xsd 的修改能够解决它:

    sed -i 's/name=\"customerName\" nillable=\"true\"/name=\"customerName\" nillable=\"false\"/g' schema/AXLSoap.xsd
    
  • 由于 CUCM 自签名证书的 HTTPS 认证验证,请求将失败,除非它被安装到 OS CA 信任库或被禁用(请参阅上面代码中的注释部分)

  • 需要以下奇怪的代码来避免发出请求时出现“不支持编译 JScript/CSharp 脚本”错误(根据此处):

    MethodInfo method = typeof( XmlSerializer ).GetMethod( "set_Mode", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static );
    method.Invoke( null, new object[] { 1 } );
    
于 2020-05-30T09:02:07.663 回答