I want to place two using directives in a EF 4.5 edmx .tt file and am having trouble. The 4.0 syntax was different and easier to manipulate.
Any help would be appreciated....
EF 4.0 syntax:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
4.5 syntax
public string UsingDirectives(bool inHeader, bool includeCollections = true)
{
return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
? string.Format(
CultureInfo.InvariantCulture,
"{0}using System;{1}" +
"{2}",
inHeader ? Environment.NewLine : "",
includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
inHeader ? "" : Environment.NewLine)
: "";
}
I need to add the following attributes in the .tt file:
EF 4.0 syntax
[Serializable]
[DataContract(IsReference = true)]
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>
{
<#
4.5 syntax
public string EntityClassOpening(EntityType entity)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1}partial class {2}{3}",
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape(entity),
_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
}
4.0 syntax
void WriteProperty(string accessibility, string type, string name, string getterAccessibility, string setterAccessibility)
{
#> [DataMember]
<#=accessibility#> <#=type#> <#=name#> { <#=getterAccessibility#>get; <#=setterAccessibility#>set; }
<#+
}
4.5 syntax
I can't find any similiar syntax**
Basically, I want the output of the class when it's regenerated from the edmx file to look like this:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization; using System.ServiceModel;
namespace YeagerTechModel
{
[Serializable]
[DataContract(IsReference = true)]
public partial class Priority
{
public Priority()
{
this.Projects = new HashSet<Project>();
}
[DataMember]
public short PriorityID { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public virtual ICollection<Project> Projects { get; set; }
}
}
Are you serious that you don't understand the code and you don't know how to change it? It is just a string formatting ... Even if you don't understand the formatting you can still update template directly as you did in the previous version so you even don't understand how text templating works. You should really spend some time on basic tutorials.
Here you have some examples
Usings:
public string UsingDirectives(bool inHeader, bool includeCollections = true)
{
var usings = new List<string>() {
"using System;",
"using System.Runtime.Serialization;",
"using System.ServiceModel;"
};
if (includeCollections) {
usings.Add("using System.Collections.Generic;");
}
return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
? string.Format(
CultureInfo.InvariantCulture,
"{0}{1}{2}",
inHeader ? Environment.NewLine : "",
String.Join(Environment.NewLine, usings),
inHeader ? "" : Environment.NewLine)
: "";
}
Class:
public string EntityClassOpening(EntityType entity)
{
const string attributes = "[Serializable, DataContract(IsReference = true)]";
return string.Format(
CultureInfo.InvariantCulture,
"{0}{1}{2} {3}partial class {4}{5}",
attributes,
Environment.NewLine,
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape(entity),
_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
}
Properties:
public string Property(EdmProperty edmProperty)
{
return string.Format(
CultureInfo.InvariantCulture,
"[DataMember] {0} {1} {2} {{ {3}get; {4}set; }}",
Accessibility.ForProperty(edmProperty),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty),
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
}
public string NavigationProperty(NavigationProperty navigationProperty)
{
var endType = _typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType());
return string.Format(
CultureInfo.InvariantCulture,
"[DataMember] {0} {1} {2} {{ {3}get; {4}set; }}",
AccessibilityAndVirtual(Accessibility.ForProperty(navigationProperty)),
navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
_code.Escape(navigationProperty),
_code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),
_code.SpaceAfter(Accessibility.ForSetter(navigationProperty)));
}
Related
The objective is to instantiate local:MyViewModel without constructor injection as well as without static resource.
The property element syntax version below works fine.
<Label.BindingContext>local:MyViewModel</Label.BindingContext>
My attempt with XML attribute syntax below also works.
<Label ... BindingContext="{local:MyViewModel}"/>
However Visual Studio Community warns me with
Type local:MyViewModel is used like a markup extension but does not derive from MarkupExtension.
Question:
Is it safe to ignore this warning?
Is there any trick to suppress the warning?
Here is the markup extension implementation for WPF. Check if it will work in Xamarin too.
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Markup;
namespace CommonCore
{
[MarkupExtensionReturnType(typeof(Type))]
public class CreateInstanceExtension : MarkupExtension
{
[ConstructorArgument("instanceType")]
[DefaultValue(null)]
public Type InstanceType { get; set; }
[ConstructorArgument("instanceValue")]
[DefaultValue(null)]
public string InstanceValue { get; set; }
public CreateInstanceExtension(Type instanceType, string instanceValue)
: this(instanceValue)
{
InstanceType = instanceType;
}
public CreateInstanceExtension(string instanceValue)
: this()
{
InstanceValue = instanceValue;
}
public CreateInstanceExtension()
{ }
private static TypeExtension typeExtension;
public override object ProvideValue(IServiceProvider serviceProvider)
{
Type type = InstanceType;
string value = InstanceValue;
if (type is null)
{
value = value?.Trim(" \t\r\n".ToCharArray());
if (!string.IsNullOrEmpty(value))
{
string typeName = null;
if (value[0] == '(')
{
int end = value.IndexOf(')');
if (end > 0)
{
typeName = value.Substring(1, end - 1);
value = value.Substring(end + 1).Trim(" \t\r\n".ToCharArray());
}
}
else
{
typeName = value;
value = null;
}
if (!string.IsNullOrEmpty(typeName))
{
(typeExtension ??= new TypeExtension()).TypeName = typeName;
type = (Type)typeExtension.ProvideValue(serviceProvider);
}
}
}
if (type is null)
{
throw new NullReferenceException("The type must be specified.");
}
if (string.IsNullOrEmpty(value))
{
return Activator.CreateInstance(type);
}
else
{
if (TypeDescriptor.GetConverter(type) is not TypeConverter converter)
{
throw new NotImplementedException("There is no TypeConverter for the specified type.");
}
var target = ((IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)))?.TargetObject as FrameworkElement;
if (target?.Language.GetEquivalentCulture() is CultureInfo culture)
return converter.ConvertFromString(null, culture, value);
return converter.ConvertFromInvariantString(value);
}
}
}
}
Usage in WPF:
<Label ... DataContext="{commcore:CreateInstance (local:MyViewModel)}"/>
<Label ... DataContext="{commcore:CreateInstance local:MyViewModel}"/>
<Label ... DataContext="{commcore:CreateInstance (sys:Double)123.456}"/>
I Create a one web API. its main aim is a request to another server and get the response from that server.
I successfully get the response for a particular server.
I get the response(its a JSON Format) is below.
{
"id": "test#gmail.com",
"active": 1,
"is_logged": true,
"token": "hsja3t56yJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlc3RAZHZlby5jb20iLCJwYXNzd29yZCI6InRlc3QyMDE4KyIsImV4cGlyZU9uIjoiMjAxOS0wNi0yMVQwNTozNzowOC4xODhaIn0.3wgGeL_HvcoEJJeEF7tj8jeXk2uIKpOoi9ewmK5yhteh",
"status": "OK",
"usertype": "TestUser",
"msg": "Login Successfull."
}
I try to separate using split function
string[] sep = response.Split(',');
foreach (string any in sep)
Console.WriteLine(any);
//string[] colon = sep[0].Split(':');
string[][] colon = sep.Select(x => x.Split(':')).ToArray();
//int count = colon.Count();
for (int i = 0; i <= colon.Length; i++)
{
Console.WriteLine(colon[i][0]);
Console.WriteLine(colon[i][1]);
}
Any other way to separate the response? I also use all the field in some other purpose.
Create a Class based on your response property:
public class UserData
{
public string id { get; set; }
public int active { get; set; }
public bool is_logged { get; set; }
public string token { get; set; }
public string status { get; set; }
public string usertype { get; set; }
public string msg { get; set; }
}
On reading the response data, use JsonConvert.DeserializeObject
string response = "{\"id\":\"test #gmail.com\",\"active\":1,\"is_logged\":true,\"token\":\"hsja3t56yJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlc3RAZHZlby5jb20iLCJwYXNzd29yZCI6InRlc3QyMDE4KyIsImV4cGlyZU9uIjoiMjAxOS0wNi0yMVQwNTozNzowOC4xODhaIn0.3wgGeL_HvcoEJJeEF7tj8jeXk2uIKpOoi9ewmK5yhteh\",\"status\":\"OK\",\"usertype\":\"TestUser\",\"msg\":\"Login Successfull.\"}";
var responseData = JsonConvert.DeserializeObject<UserData>(response);
//here the print in JSON Data
Console.WriteLine("id : " + responseData.id);
Console.WriteLine("active : " + responseData.active);
Console.WriteLine("is_logged : " + responseData.is_logged);
Console.WriteLine("token : " + responseData.token);
Console.WriteLine("status : " + responseData.status);
Console.WriteLine("usertype : " + responseData.usertype);
Console.WriteLine("msg : " + responseData.msg);
use Newtonsoft.json.dll by adding the NuGet package,
Then convert the response to json object
JObject jo = JObject.Parse(searchCondition);
foreach (JToken child in jo.Children()) {
var prop = child as JProperty;
if (prop.Value != null && !string.IsNullOrEmpty(prop.Value.ToString())) {
string name=prop.Name;
string value = prop.Value;
//You can now do whatever with the values like put in a list of object
}
}
This is My own example to get the properties from JSON string, you can use this.
But first, you need to install this package:-> Newtonsoft.Json.Linq to access JObject
using System;
using Newtonsoft.Json.Linq;
public class Program
{
public static void Main()
{
string jsonString = "{\"firstname\":\"Alex Wu\",\"lastname\":\"type\"}";
JObject jObject = JObject.Parse(jsonString);
string firstname = (string)jObject.SelectToken("firstname");
string lastname = (string)
Console.WriteLine("{0}", firstname);
Console.ReadLine();
}
}
I'm using the latest version of WebApi and OData and everything is set up to work right. The only problem is when I try to use $select .
It throws the error bellow
Object of type 'System.Linq.EnumerableQuery`1[System.Web.OData.Query.Expressions.SelectExpandBinder+SelectAll`1[WebApplication1.Controllers.Person]]' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[WebApplication1.Controllers.Person]'.
I looked at the documentation and their suggestion is to use [Queryable] on top of the Get method in the controller or the in WebApiConfig to use config.EnableQuerySupport and neither of these are available options. I'm currently using [EnableQuery]
EDIT
OdataController:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.OData;
using System.Xml.Serialization;
namespace WebApplication1.Controllers
{
public class PeopleController : ODataController
{
// GET api/values
[EnableQuery]
public IQueryable<Person> Get()
{
return new Person[] { new Person()
{
Id = 1,
FirstName = "Testing",
LastName = "2"
}, new Person()
{
Id = 2,
FirstName = "TestTest",
LastName = "3"
} }.AsQueryable();
}
// GET api/values/5
public Person Get(int id)
{
return new Person()
{
Id = 3,
FirstName = "Test",
LastName = "1"
};
}
// POST api/values
public void Post([FromBody]Person value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]Person value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
public class Person
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
WebApiConfig
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
using System.Web.OData.Formatter;
using WebApplication1.Controllers;
namespace WebApplication1
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
var odataFormatters = ODataMediaTypeFormatters.Create();
config.Formatters.InsertRange(0, odataFormatters);
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Person>("People");
config.AddODataQueryFilter();
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "api",
model: builder.GetEdmModel());
}
}
}
UPDATE 2
seems to only throw an error retrieving the data in xml format. Json seems to work
This is a known limitation of the XmlMediaTypeFormatter class from the System.Net.Formatting Nuget package. The implementation of the JSON formatter does support the $select and $expand commands but these are not available when content negotiation determines that XML should be returned.
You should look into implementing OData endpoints (as opposed to WebAPI endpoints) should you need to return XML formatted responses. More information on how this can be done can be found here:
http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options
Found a solution. It isn't perfect but it does work!
Maybe it will be useful for someone because I've spent on it few hours of research and trying.
Step #1 create custom xml formatter:
public class CustomXmlFormatter : MediaTypeFormatter
{
private JsonMediaTypeFormatter jFormatter = null;
public CustomXmlFormatter(JsonMediaTypeFormatter jFormatter)
{
SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/xml"));
this.jFormatter = jFormatter;
}
public override bool CanReadType(Type type)
{
return false;
}
public override bool CanWriteType(Type type)
{
return true;
}
public override Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext)
{
return Task.Factory.StartNew(() =>
{
using (MemoryStream ms = new MemoryStream())
{
var tsk = jFormatter.WriteToStreamAsync(type, value, ms, content, transportContext);
tsk.Wait();
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
var xDoc = XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(ms, new XmlDictionaryReaderQuotas()));
using (XmlWriter xw = XmlWriter.Create(writeStream))
{
xDoc.WriteTo(xw);
}
}
});
}
}
Step #2 register it in startup section:
var formatters = ODataMediaTypeFormatters.Create();
var jsonFormatter = config.Formatters.JsonFormatter;
var customXmlFormatter = new CustomXmlFormatter(jsonFormatter);
customXmlFormatter.AddQueryStringMapping("$format", "cxml", "application/xml");
config.Formatters.Add(customXmlFormatter);
use it as
http://url..../actionName?$format=cxml&$select=ObjectName,ObjectId
I have a solution that i need to call a console app from asp.net and need to pass variables. one variable is a generic list of a certain class.
I have tried passing it but I got error that i cannot convert a generic list to a string which is correct.
I am not sure if there is another way to pass this.
I know webservice can solve this issue. But it there any other options?
Is this possible to do or only string are possible to pass
Here is the generic list sample.
List<person> personList = new List<person>();
person p = new person();
p.name = "test";
p.age = 12;
p.birthdate = 01/01/2014
personList.add(p)
Thanks.
Ok, Console application accepts only strings. This is defined in the Main method as
static void Main(string[] args)
Since you have a complex object list it'll be bit hard to pass this information to the console application (but not impossible). There are several options for you.
Pass your values as comma separated values as a string as long as this string is not too long.
Web Services or a Web API as you suggested.
Serialize your object to an XML file and then deserialize in your console application.
Write and read from a persistent data store
UPDATE
Sample Code for Option 3 (Write to an XML file)
I wrote this sample code out of curiosity. Hope this helps to solve your issue.
ASP.Net Website
I have a button in my web page (Default.aspx) and in it's click event it writes the Person collection/ List to an XML file. Here's the code behind.
using System;
using System.IO;
using System.Xml.Serialization;
namespace WriteToConsole
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnWriteToConsole_Click(object sender, EventArgs e)
{
PersonCollection personList = new PersonCollection();
// Person 1
Person p = new Person();
p.Name = "test 1";
p.Age = 12;
p.BirthDate = DateTime.Parse("01/01/2014");
personList.Add(p);
// Person 2
Person p2 = new Person();
p2.Name = "test 2";
p2.Age = 25;
p2.BirthDate = DateTime.Parse("01/01/2014");
personList.Add(p2);
XmlSerializer serializer = new XmlSerializer(personList.GetType());
StreamWriter file = new StreamWriter(#"D:\temp\PersonCollection.xml");
serializer.Serialize(file, personList);
file.Close();
}
}
}
And, the Person.cs looks like this.
using System;
using System.Collections.Generic;
namespace WriteToConsole
{
[Serializable]
[System.Xml.Serialization.XmlRoot("PersonCollection")]
public class PersonCollection : List<Person> {
}
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime BirthDate { get; set; }
public Person()
{
this.Name = string.Empty;
this.Age = 0;
this.BirthDate = DateTime.MinValue;
}
}
}
Console Application
Then read the XML file in your console application and display the data in personList on the console.
using System;
using System.IO;
using System.Xml.Serialization;
namespace ReadInConsole
{
class Program
{
static void Main(string[] args)
{
XmlSerializer deserializer = new XmlSerializer(typeof(PersonCollection));
TextReader textReader = new StreamReader(#"D:\temp\PersonCollection.xml");
PersonCollection personList = new PersonCollection();
personList = (PersonCollection)deserializer.Deserialize(textReader);
textReader.Close();
if (personList != null && personList.Count > 0)
{
foreach (Person p in personList)
{
Console.WriteLine("Person name: {0}, Age: {1} and DOB: {2}", p.Name, p.Age, p.BirthDate.ToShortDateString());
}
Console.ReadLine();
}
}
}
}
In your console application you should have the same Person class as a modal (This is same as the Person class in your Web Application. Only the namespace is different).
using System;
using System.Collections.Generic;
namespace ReadInConsole
{
[Serializable]
[System.Xml.Serialization.XmlRoot("PersonCollection")]
public class PersonCollection : List<Person>
{
}
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime BirthDate { get; set; }
public Person()
{
this.Name = string.Empty;
this.Age = 0;
this.BirthDate = DateTime.MinValue;
}
}
}
Hope you understand the code.
I am having an XML string like
<?xml version="1.0"?>
<FullServiceAddressCorrectionDelivery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AuthenticationInfo xmlns="http://www.usps.com/postalone/services/UserAuthenticationSchema">
<UserId xmlns="">FAPushService</UserId>
<UserPassword xmlns="">Password4Now</UserPassword>
</AuthenticationInfo>
</FullServiceAddressCorrectionDelivery>
In Order to map the nodes with Class, i am having the class structure in WCF like
[DataContract]
[Serializable]
public class FullServiceAddressCorrectionDelivery
{
[XmlElement("AuthenticationInfo", Namespace = "http://www.usps.com/postalone/services/UserAuthenticationSchema")]
[DataMember]
public AuthenticationInfo AuthenticationInfo { get; set; }
}
[DataContract]
[Serializable]
public class AuthenticationInfo
{
[DataMember]
[XmlElement("UserId", Namespace = "")]
public string UserId { get; set; }
[DataMember]
[XmlElement("UserPassword", Namespace = "")]
public string UserPassword { get; set; }
}
For De-serialization , i used xmlserializer to De-serialize the object
XmlSerializer xs = new XmlSerializer(typeof(FullServiceAddressCorrectionDelivery));
var result = (FullServiceAddressCorrectionDelivery)xs.Deserialize(stream);
this method returned me a Null FullServiceAddressCorrectionDelivery object..
but when i used DataContractSerializer .. like
DataContractSerializer xs = new DataContractSerializer(typeof(FullServiceAddressCorrectionDelivery));
var result = (FullServiceAddressCorrectionDelivery)xs.ReadObject(stream);
then following exception came out..
Error in line 2 position 46. Expecting element 'FullServiceAddressCorrectionDelivery' from namespace 'http://schemas.datacontract.org/2004/07/WcfService1'.. Encountered 'Element' with name 'FullServiceAddressCorrectionDelivery', namespace ''.
i am stuck with this...
i somehow with the help of RENE(StackOverFlow member) succeded to deserialize if the class was in same project .. but when i converted them to WCF Datacontracts.. i came across that issue ..... please guide me where i am doing wrong here...
Depending on how you want to use the data contract serializer (DCS) for that input, you may or may not be able to do that. The namespace of the data members in the DCS are defined by the namespace of the contract to which they belong, unless it's the root element (in which case the [DC] namespace also defines the namespace of the element).
Your root element, FullServiceAddressCorrectionDelivery, has one namespace in your sample XML (empty), and its member, AuthenticationInfo, has another (http://www.usps.com/postalone...). That means that unless you actually change how the serializer is created, you won't be able to use the DCS for this type.
The XmlSerializer (XS), however, should work just fine - members of the type can have different namespaces. As you can see in the code below, I can post the XML you provided verbatim to an operation which takes the FullServiceAddressCorrectionDelivery as an input, and the object is properly populated - you need to mark the operation (or the contract) with the [XmlSerializerFormat] attribute to get this behavior.
public class Post_6fc3a1bd_b3ca_48da_b4d2_35271135ed8a
{
const string XML = #"<?xml version=""1.0""?>
<FullServiceAddressCorrectionDelivery xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<AuthenticationInfo xmlns=""http://www.usps.com/postalone/services/UserAuthenticationSchema"">
<UserId xmlns="""">FAPushService</UserId>
<UserPassword xmlns="""">Password4Now</UserPassword>
</AuthenticationInfo>
</FullServiceAddressCorrectionDelivery>";
[XmlRoot(ElementName = "FullServiceAddressCorrectionDelivery", Namespace = "")]
public class FullServiceAddressCorrectionDelivery
{
[XmlElement("AuthenticationInfo", Namespace = "http://www.usps.com/postalone/services/UserAuthenticationSchema")]
[DataMember]
public AuthenticationInfo AuthenticationInfo { get; set; }
}
public class AuthenticationInfo
{
[XmlElement("UserId", Namespace = "")]
public string UserId { get; set; }
[XmlElement("UserPassword", Namespace = "")]
public string UserPassword { get; set; }
}
[ServiceContract(Namespace = "")]
public interface ITest
{
[XmlSerializerFormat]
[OperationContract]
FullServiceAddressCorrectionDelivery Echo(FullServiceAddressCorrectionDelivery input);
}
public class Service : ITest
{
public FullServiceAddressCorrectionDelivery Echo(FullServiceAddressCorrectionDelivery input)
{
return input;
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
host.Open();
Console.WriteLine("Host opened");
WebClient c = new WebClient();
c.Headers[HttpRequestHeader.ContentType] = "text/xml";
Console.WriteLine(c.UploadString(baseAddress + "/Echo", XML));
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
For completeness sake, this is how you'd change the serializer creation (passing the root name and namespace) to be able to deserialize the XML you provided using the data contract serializer.
public class Post_6fc3a1bd_b3ca_48da_b4d2_35271135ed8a_b
{
const string XML = #"<?xml version=""1.0""?>
<FullServiceAddressCorrectionDelivery xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<AuthenticationInfo xmlns=""http://www.usps.com/postalone/services/UserAuthenticationSchema"">
<UserId xmlns="""">FAPushService</UserId>
<UserPassword xmlns="""">Password4Now</UserPassword>
</AuthenticationInfo>
</FullServiceAddressCorrectionDelivery>";
[DataContract(Name = "FullServiceAddressCorrectionDelivery", Namespace = "http://www.usps.com/postalone/services/UserAuthenticationSchema")]
public class FullServiceAddressCorrectionDelivery
{
[DataMember]
public AuthenticationInfo AuthenticationInfo { get; set; }
}
[DataContract(Name = "AuthenticationInfo", Namespace = "")]
public class AuthenticationInfo
{
[DataMember]
public string UserId { get; set; }
[DataMember]
public string UserPassword { get; set; }
}
static string Serialize(object obj, bool useDataContractSerializer)
{
MemoryStream ms = new MemoryStream();
XmlWriterSettings ws = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
OmitXmlDeclaration = false,
Encoding = new UTF8Encoding(false)
};
XmlWriter w = XmlWriter.Create(ms, ws);
if (useDataContractSerializer)
{
var dcs = new DataContractSerializer(obj.GetType(), "FullServiceAddressCorrectionDelivery", "");
dcs.WriteObject(w, obj);
}
else
{
new XmlSerializer(obj.GetType()).Serialize(w, obj);
}
w.Flush();
string result = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(result);
w.Close();
ms.Close();
return result;
}
public static void Test()
{
Console.WriteLine("Serialization:");
MemoryStream ms = new MemoryStream();
XmlWriterSettings ws = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
OmitXmlDeclaration = false,
Encoding = new UTF8Encoding(false)
};
XmlWriter w = XmlWriter.Create(ms, ws);
var dcs = new DataContractSerializer(typeof(FullServiceAddressCorrectionDelivery), "FullServiceAddressCorrectionDelivery", "");
var obj = new FullServiceAddressCorrectionDelivery
{
AuthenticationInfo = new AuthenticationInfo
{
UserId = "FAPushService",
UserPassword = "Password4Now"
}
};
dcs.WriteObject(w, obj);
w.Flush();
string result = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(result);
Console.WriteLine();
Console.WriteLine("Deserialization:");
ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
var obj2 = (FullServiceAddressCorrectionDelivery)dcs.ReadObject(ms);
Console.WriteLine("{0} - {1}", obj2.AuthenticationInfo.UserId, obj2.AuthenticationInfo.UserPassword);
}
}