Class serialization and namespace (xmlns) - wcf

I have a class that I am serializing
public partial class Security : MessageHeader
{
private Assertion assertionField;
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:tc:SAML:2.0:assertion")]
public Assertion Assertion
{
get
{
return this.assertionField;
}
set
{
this.assertionField = value;
}
}
public override string Name
{
get { return "Security"; }
}
public override string Namespace
{
get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; }
}
[XmlIgnoreAttribute]
public string UserID { get; set; }
[XmlIgnoreAttribute]
public string FirstName { get; set; }
[XmlIgnoreAttribute]
public string LastName { get; set; }
[XmlIgnoreAttribute]
public string ReasonForSearch { get; set; }
public Security()
{
Assertion = new Assertion(UserID, FirstName, LastName, ReasonForSearch);
}
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
XmlSerializer serializer = new XmlSerializer(typeof(Assertion));
serializer.Serialize(writer, Assertion, ns);
}
}
this is how i am adding code header
using (OperationContextScope scope = new OperationContextScope(healthixClient.InnerChannel))
{
Security msgHdr = new Security();
msgHdr.UserID = "TestUserID";
msgHdr.FirstName = "TestUserFirstName";
msgHdr.LastName = "TestUserLastName";
msgHdr.ReasonForSearch = "ReasonForSearch";
OperationContext.Current.OutgoingMessageHeaders.Add(msgHdr);
}
when i serialize this and add in my code header it looks like this
<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<saml:Assertion ID="saml_6691a2b1-2a08-4d10-9d90-b006727d0e02" IssueInstant="2013-09-09T15:38:16Z" Version="2.0" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
< rest of the Xml is correct >
Now if I only change my override OnWriteHeaderContents method to
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
XmlSerializer serializer = new XmlSerializer(typeof(Security));
serializer.Serialize(writer, new Security(), ns);
}
the header looks like this
<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<Security xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
<saml:Assertion ID="saml_6691a2b1-2a08-4d10-9d90-b006727d0e02" IssueInstant="2013-09-09T15:29:09Z" Version="2.0">
< rest of the Xml is correct >
What i want the header to look like is this
<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
<saml:Assertion ID="saml_6691a2b1-2a08-4d10-9d90-b006727d0e02" IssueInstant="2013-07-29T20:17:30.846Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">

Try this option in OnWriteHeaderContents method
writer.WriteStartElement("saml", "Assertion", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.WriteString("Value");
writer.WriteEndElement();

Related

How to use the TempData attribute to pass data to a Razor view

I have:
namespace Test
{
public interface ITest
{
public string Test1(string s);
}
public class Test : ITest
{
[Microsoft.AspNetCore.Mvc.TempData]
public string Message
{
get; set;
}
public string Test1(string s)
{
Message = "Test " + s;
return "Test has run";
}
}
}
And in Startup.cs:
services.AddScoped<Test.ITest, Test.Test>();
Then in a Razor view:
#inject Test.ITest Test
<p>Result is #Test.Test1("Hello World!")</p>
<p>TempData["Message"] is #TempData["Message"]</p>
The output is:
Result is Test has run
TempData["Message"] is
Where have I done incorrectly? How can I pass a TempData["Message"] from some code (that is not in a Controller) to a Razor page?
How can I pass a TempData["Message"] from some code (that is not in a Controller) to a Razor page?
If you'd like to retain and pass value via TempData inside your custom service, you can try the following code snippet.
public interface ITest
{
public string Test1(string s);
public string Test2();
}
public class Test : ITest
{
private readonly IHttpContextAccessor _httpContextAccessor;
public Test(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string Message
{
get {
var tempDataDictionaryFactory = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<ITempDataDictionaryFactory>();
var tempDataDictionary = tempDataDictionaryFactory.GetTempData(_httpContextAccessor.HttpContext);
if (tempDataDictionary.TryGetValue("Message", out object value))
{
return (string)value;
};
return "";
}
set
{
var tempDataDictionaryFactory = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<ITempDataDictionaryFactory>();
var tempDataDictionary = tempDataDictionaryFactory.GetTempData(_httpContextAccessor.HttpContext);
tempDataDictionary.Remove("Message");
tempDataDictionary.TryAdd("Message", value);
}
}
public string Test1(string s)
{
Message = "Test " + s;
return "Test has run";
}
public string Test2()
{
return Message;
}
}
In Razor Page
<p>Result is #Test.Test1("Hello World!")</p>
<p>TempData["Message"] is #TempData.Peek("Message")</p>
<p>#Test.Test2()</p>
Test Result

registering PDX type in type registry with Geode C# native client

I am trying to implement a very simple PDX autoserialization in Geode. I've created a domain class of my own with a zero arg constructor:
public class TestPdx
{
public string Test1 { get; set; }
public string Test2 { get; set; }
public string Test3 { get; set; }
public TestPdx() { }
}
Now I want this class to auto serialize. I start a server cache with the following cache.xml where I attempt to register this type for auto PDX:
<?xml version="1.0" encoding="UTF-8"?>
<cache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://geode.apache.org/schema/cache"
xsi:schemaLocation="http://geode.apache.org/schema/cache
http://geode.apache.org/schema/cache/cache-1.0.xsd"
version="1.0">
<cache-server/>
<pdx>
<pdx-serializer>
<class-name>org.apache.geode.pdx.ReflectionBasedAutoSerializer</class-name>
<parameter name="classes"><string>TestPdx</string></parameter>
</pdx-serializer>
</pdx>
<region name="webclient" refid="REPLICATE_PERSISTENT"/>
</cache>
and then run the following code:
static void Main(string[] args)
{
// 1. cache
CacheFactory cacheFactory = CacheFactory.CreateCacheFactory();
Cache cache = cacheFactory
.SetSubscriptionEnabled(true)
.SetPdxReadSerialized(true)
.Create();
Serializable.RegisterPdxSerializer(new ReflectionBasedAutoSerializer());
RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY);
IRegion<string, TestPdx> region = regionFactory.Create<string, TestPdx>("webclient");
// 3. TestPx object
TestPdx t = new TestPdx();
t.Test1 = "test1";
t.Test2 = "test2";
t.Test3 = "test3";
region["1"] = t;
// 4. Get the entries
TestPdx result1 = region["1"];
// 5. Print result
Console.WriteLine(result1.Test1);
Console.WriteLine(result1.Test2);
Console.WriteLine(result1.Test3);
}
This code is crashing at line region["1"] = t; with error
GFCLI_EXCEPTION:System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at apache.geode.client.SerializationRegistry.GetPDXIdForType(SByte* , SharedPtr<apache::geode::client::Serializable>* )
So I haven't registered the PDX type properly. How do you do that with native client?
THANKS
An answer here is to implement IPdxSerializable in the TestPdx as follows:
public class TestPdx : IPdxSerializable
{
public string Test1 { get; set; }
public string Test2 { get; set; }
public string Test3 { get; set; }
public int Pid { get; set; }
public void ToData(IPdxWriter writer)
{
writer.WriteString("Test1", Test1);
writer.WriteString("Test2", Test2);
writer.WriteString("Test3", Test3);
writer.WriteInt("Pid", Pid);
writer.MarkIdentityField("Pid");
}
public void FromData(IPdxReader reader)
{
Test1 = reader.readString("Test1");
Test2 = reader.readString("Test2");
Test3 = reader.readString("Test3");
Pid = reader.readInt("Pid");
}
public static IPdxSerializable CreateDeserializable()
{
return new TestPdx();
}
public TestPdx() { }
}
and then register the Pdx type in the Geode, and use a region of type object or type TestPdx as follows:
Serializable.RegisterPdxType(TestPdx.CreateDeserializable);
IRegion<string, Object> t = regionFactory.Create<string, Object>("test");
and to write the TestPdx to the region simply:
TestPdx value = new TestPdx();
value.Test1 = "hello";
value.Test2 = "world";
value.Test3 = "again";
t[key] = value;
and there will be a PdxInstance in the Geode region so you can run OQL queries on it, etc.

What's the best way to serialize an array based on an interface in WCF?

First the code:
[ServiceContract]
public interface IWorker
{
[OperationContract]
void Process(XmlElement data);
[OperationContract]
void Update(Rule rule);
}
[DataContract]
public class Rule
{
[OperationContract]
public string Expression { get; set; }
[OperationContract]
public List<IAction> Actions { get; set; }
}
public interface IAction
{
void Execute(XmlElement data);
}
A dispatcher encodes data as xml and sends it to an IWorker instance where each expression is evaluated. When an IWorker instance evaluates an expression as true, IAction.Execute is called and the xml/data is passed.
What's the best way to serialize Rule.Actions? I've started writing a custom serializer but I'd prefer to see if there is an easier way.
Thanks.
I dont think you can use interfaces in DataContracts (someone correct me if im wrong, but i assume thats like trying to use a generic too). What I do, is have a parent class, then add the KnownType attribute. For instance
[DataContract]
public class Action
{
//members and properties
}
[DataContract]
public class SomeOtherAction:Action
{
//more implimentation
}
[DataContract]
[KnownType(typeof(SomeOtherAction))]
public class Rule
{
[DataMember]
List<Action> Actions{get;set;}
}
Now you can stuff any object that inherits from the parent Action object in to the Actions list, and it will properly serialize all their respective class properties (as long as the object is listed as a knowntype).
*I used "Action" name as an example to relate to yours, obviously Action is a keyword in .NET
Serialization is the process of converting between an object data and bytes which can be transferred over the wire. Interfaces define behavior, so by default WCF can't serialize such data. If you have the exact same assemblies on the client and the server, however, you can use the NetDataContractSerializer, which will essentially serialize (and be able to serialize) all the type information for the objects being serialized, so it can be recreated at the other side.
The code below shows how to use the NetDataContractSerializer in a service for that (based on the main example for this, the post from Aaron Skonnard at http://www.pluralsight-training.net/community/blogs/aaron/archive/2006/04/21/22284.aspx)
public class StackOverflow_6932356
{
[ServiceContract]
public interface IWorker
{
[OperationContract]
void Process(XmlElement data);
[OperationContract]
void Update(Rule rule);
}
[DataContract]
public class Rule
{
[DataMember]
public string Expression { get; set; }
[DataMember]
public List<IAction> Actions { get; set; }
}
public interface IAction
{
void Execute(XmlElement data);
}
public class Service : IWorker
{
static List<IAction> AllActions = new List<IAction>();
public void Process(XmlElement data)
{
foreach (var action in AllActions)
{
action.Execute(data);
}
}
public void Update(Rule rule)
{
AllActions = rule.Actions;
}
}
public class Action1 : IAction
{
public void Execute(XmlElement data)
{
Console.WriteLine("Executing {0} for data: {1}", this.GetType().Name, data.OuterXml);
}
}
public class Action2 : IAction
{
public void Execute(XmlElement data)
{
Console.WriteLine("Executing {0} for data: {1}", this.GetType().Name, data.OuterXml);
}
}
class NetDataContractSerializerOperationBehavior : DataContractSerializerOperationBehavior
{
public NetDataContractSerializerOperationBehavior(OperationDescription operationDescription)
: base(operationDescription) { }
public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
{
return new NetDataContractSerializer(name, ns);
}
public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
{
return new NetDataContractSerializer(name, ns);
}
}
static void ReplaceDCSOB(ServiceEndpoint endpoint)
{
foreach (var operation in endpoint.Contract.Operations)
{
for (int i = 0; i < operation.Behaviors.Count; i++)
{
if (operation.Behaviors[i] is DataContractSerializerOperationBehavior)
{
operation.Behaviors[i] = new NetDataContractSerializerOperationBehavior(operation);
break;
}
}
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IWorker), new BasicHttpBinding(), "");
ReplaceDCSOB(endpoint);
host.Open();
Console.WriteLine("Host opened");
var factory = new ChannelFactory<IWorker>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
ReplaceDCSOB(factory.Endpoint);
var proxy = factory.CreateChannel();
proxy.Update(new Rule
{
Expression = "Expr",
Actions = new List<IAction> { new Action1(), new Action2() }
});
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root><foo>bar</foo></root>");
proxy.Process(doc.DocumentElement);
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}

WCF: How to define a DataContract class for an extreamly simple response? Need to deserialize Text of the root element

I would like to call Sql Azure's REST API to create a SQL Azure server. The method is documented here: http://msdn.microsoft.com/en-us/library/gg715274.aspx
I ran into problem. The response from this method is very simple:
<?xml version="1.0" encoding="utf-8"?><ServerName xmlns="http://schemas.microsoft.com/sqlazure/2010/12/">zpc0fbxur0</ServerName>
How do I define a DataContract class for this response?
If response were something like this:
<?xml version="1.0" encoding="utf-8"?>
<ServerName xmlns="http://schemas.microsoft.com/sqlazure/2010/12/"><Name>zpc0fbxur0</Name></ServerName>
the following class would work:
`
[DataContract(Namespace=SqlAzureConstants.ManagementNS, Name="ServerName")]
public class ServerName : IExtensibleDataObject
{
[DataMember()]
public string Name
{
get;
set;
}
public ExtensionDataObject ExtensionData
{
get;
set;
}
}
`
But I need to specify that property should be mapped to the text of the root element. Any ideas how to do this?
The DataContractSerializer as it's created by default cannot deserialize that XML - but if you use a constructor which sets the rootName and rootNamespace parameters, it can be done.
Another alternative is to use the XmlSerializer, where you can use it directly.
The code below shows both options, and also a WebChannelFactory implementation which uses the XmlSerializer type.
public class StackOverflow_6399085
{
[XmlRoot(ElementName = "ServerName", Namespace = "http://schemas.microsoft.com/sqlazure/2010/12/")]
public class ServerName
{
[XmlText]
public string Name { get; set; }
public override string ToString()
{
return string.Format("ServerName[Name={0}]", this.Name);
}
}
const string XML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ServerName xmlns=\"http://schemas.microsoft.com/sqlazure/2010/12/\">zpc0fbxur0</ServerName>";
static void RunWithXmlSerializer()
{
XmlSerializer xs = new XmlSerializer(typeof(ServerName));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
ServerName obj = (ServerName)xs.Deserialize(ms);
Console.WriteLine("Using XML serializer: {0}", obj);
}
static void RunWithDataContractSerializer()
{
DataContractSerializer dcs = new DataContractSerializer(typeof(string), "ServerName", "http://schemas.microsoft.com/sqlazure/2010/12/");
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
string name = (string)dcs.ReadObject(ms);
Console.WriteLine("Using DataContractSerializer (different name): {0}", name);
}
[ServiceContract(Namespace = "http://schemas.microsoft.com/sqlazure/2010/12/")]
public class MockSqlAzureRestService
{
[WebGet]
public Stream GetServerName()
{
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
return ms;
}
}
[ServiceContract(Namespace = "http://schemas.microsoft.com/sqlazure/2010/12/")]
public interface IServerNameClient
{
[WebGet(BodyStyle = WebMessageBodyStyle.Bare)]
[XmlSerializerFormat]
ServerName GetServerName();
}
static void RunWithWCFRestClient()
{
// Setting up the mock service
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
WebServiceHost host = new WebServiceHost(typeof(MockSqlAzureRestService), new Uri(baseAddress));
host.Open();
WebChannelFactory<IServerNameClient> factory = new WebChannelFactory<IServerNameClient>(new Uri(baseAddress));
IServerNameClient proxy = factory.CreateChannel();
var name = proxy.GetServerName();
Console.WriteLine("Using WCF REST client: {0}", name);
}
public static void Test()
{
RunWithXmlSerializer();
RunWithDataContractSerializer();
RunWithWCFRestClient();
}
}

WCF response leaving out <xml header>

I am finally starting to get somewhere with WCF, but I have run into another problem. The response sent back does not include the header
<?xml version="1.0" encoding="utf-8" ?>
My Service Contract
[ServiceContract]
public interface IService1
{
// you can have optional parameters by simply specifying them and they will return null if there is nothing in there
[WebGet(UriTemplate="testing={value}", ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
XElement GetData(string value);
}
[XmlSerializerFormat]
public class Service1 : IService1
{
public XElement GetData(string value)
{
return new XElement("Somename", value);
}
}
returns this (3 is the value specified)
<Somename>3</Somename>
Is it also possible to easily wrap a response in a root element? Something like <response></response>?
responsThe result of calling the GetData method is the contents of what you return in the method. If you want a wrapper then return something like:
[XmlSerializerFormat]
public class Service1 : IService1
{
public XElement GetData(string value)
{
return new XElement("response",
new XElement("Somename", value));
}
}
EDIT:
To add the XML declaration (which actually may not be a good idea but you know best) do something like this:
var doc = new XDocument(
new XElement("response",
new XElement("Somename", value)));
doc.Declaration = new XDeclaration("1.0", "utf-8", "true");
return doc.Root;
I was looking for an answer to this myself, and stumbled upon this article:
http://shevaspace.blogspot.com/2009/01/include-xml-declaration-in-wcf-restful.html
It solved the problem in my situation, perhaps it is of help for others as well. It describes a custom attribute IncludeXmlDeclaration that you can stick on your method, and it will output the xml header.
For completeness, here is the code copied from the article:
public class XmlDeclarationMessage : Message
{
private Message message;
public XmlDeclarationMessage(Message message)
{
this.message = message;
}
public override MessageHeaders Headers
{
get { return message.Headers; }
}
protected override void OnWriteBodyContents(System.Xml.XmlDictionaryWriter writer)
{
// WCF XML serialization doesn't support emitting XML DOCTYPE, you need to roll up your own here.
writer.WriteStartDocument();
message.WriteBodyContents(writer);
}
public override MessageProperties Properties
{
get { return message.Properties; }
}
public override MessageVersion Version
{
get { return message.Version; }
}
}
public class XmlDeclarationMessageFormatter : IDispatchMessageFormatter
{
private IDispatchMessageFormatter formatter;
public XmlDeclarationMessageFormatter(IDispatchMessageFormatter formatter)
{
this.formatter = formatter;
}
public void DeserializeRequest(Message message, object[] parameters)
{
formatter.DeserializeRequest(message, parameters);
}
public Message SerializeReply(MessageVersion messageVersion, Object[] parameters, Object result)
{
var message = formatter.SerializeReply(messageVersion, parameters, result);
return new XmlDeclarationMessage(message);
}
}
public class IncludeXmlDeclarationAttribute : Attribute, IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.Formatter = new XmlDeclarationMessageFormatter(dispatchOperation.Formatter);
}
public void Validate(OperationDescription operationDescription)
{
}
}