WCF Service Implementation
When interfacing with WCF services from Python using the Suds library, proper parameter serialization is essential for successful communication. The service contract defines various operations with different parameter types, including custom data contracts and collections.
Service Contract Definition
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
namespace WcfServiceDemo
{
[ServiceContract]
public interface IServiceDemo
{
[OperationContract]
string ProcessSimpleValue(string input);
[OperationContract]
List<dataitem> ProcessItemList(List<dataitem> items);
[OperationContract]
DataItem ProcessSingleItem(DataItem item);
[OperationContract]
Dictionary<string string=""> ProcessStringDictionary(Dictionary<string string=""> dictionary);
[OperationContract]
Dictionary<string dictionary="" int="">[]> ProcessNestedDictionary(Dictionary<string dictionary="" int="">[]> complexDict);
}
[DataContract]
public class MenuOption
{
[DataMember]
public string DisplayName { get; set; }
[DataMember]
public string OptionValue { get; set; }
}
[DataContract]
public class DataItem
{
[DataMember]
public List<menuoption> MenuOptions { get; set; }
}
}
</menuoption></string></string></string></string></dataitem></dataitem>
Service Implementation
namespace WcfServiceDemo
{
public class ServiceDemo : IServiceDemo
{
public string ProcessSimpleValue(string input)
{
return input;
}
public List<dataitem> ProcessItemList(List<dataitem> items)
{
return items;
}
public DataItem ProcessSingleItem(DataItem item)
{
return item;
}
public Dictionary<string string=""> ProcessStringDictionary(Dictionary<string string=""> dictionary)
{
return dictionary;
}
public Dictionary<string dictionary="" int="">[]> ProcessNestedDictionary(Dictionary<string dictionary="" int="">[]> complexDict)
{
return complexDict;
}
}
}
</string></string></string></string></dataitem></dataitem>
Python Client Implementation
Using Suds to interact with WCF services requires careful construction of complex parameters. The client factory creates appropriate objects that match the WCF service expectations.
Basic Client Setup
from suds.client import Client
service_client = Client('http://localhost:8001/ServiceDemo/?singleWsdl')
# Simple parameter example
simple_result = service_client.service.ProcessSimpleValue('test_input')
print(simple_result)
Complex Parameter Construction
# Creating nested dictionary structure
string_int_pair = service_client.factory.create('ns2:KeyValueOfstringint')
string_int_pair.Key = 'sample_key'
string_int_pair.Value = 42
pair_collection = service_client.factory.create('ns2:ArrayOfKeyValueOfstringint')
pair_collection.KeyValueOfstringint = [string_int_pair]
nested_collection = service_client.factory.create('ns2:ArrayOfArrayOfKeyValueOfstringint')
nested_collection.ArrayOfKeyValueOfstringint = [pair_collection]
complex_pair = service_client.factory.create('ns2:KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1')
complex_pair.Key = 'outer_key'
complex_pair.Value = nested_collection
final_structure = service_client.factory.create('ns2:ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1')
final_structure.KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1 = complex_pair
# Calling service with complex parameter
result = service_client.service.ProcessNestedDictionary(final_structure)
print(result.KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1[0].Key)
Custom Object Creation
# Creating custom data contract objects
menu_option = service_client.factory.create('ns0:MenuOption')
menu_option.DisplayName = 'Option1'
menu_option.OptionValue = 'Value1'
option_list = service_client.factory.create('ns0:ArrayOfMenuOption')
option_list.MenuOption = [menu_option]
data_item = service_client.factory.create('ns0:DataItem')
data_item.MenuOptions = option_list
# Service call with custom object
item_result = service_client.service.ProcessSingleItem(data_item)
print(item_result.MenuOptions.MenuOption[0].DisplayName)
Configuration Considerations
Proper WCF configuraton is crucial for successful communication. The binding type must match betwean service and client configurations. BasicHttpBinding is recommended for interoperability with Python clients.
<endpoint address=""
contract="WcfServiceDemo.IServiceDemo"
binding="basicHttpBinding">
</endpoint>
Using wsHttpBinding may cause content type mismatches between the Python client and WCF service, resulting in communication errors.