remove namespace's prefix in datacontract - c#

My problem is the following :
I'm creating a SOAP service using WCF and I'm testing it using a chrome plugin called Boomerang SOAP & Rest Client (it create a request by loading your wsdl, you just have to change values).
But everytime my datamembers have namespace's prefix and i don't want them.
the request should look like this :
And i get this :
here is my service :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;
using Ory_Soap.DTO;
namespace Ory_Soap
{
[ServiceContract (Namespace = "http://my.company.com")]
[XmlSerializerFormat]
public interface FeedbackReceiverSOAP {
[OperationContract]
[WebInvoke]
void receiveProvisioningFeedback(ProvisioningFeedback provisioningFeedback);
}
[DataContract(Namespace = "http://my.company.com")]
public class ProvisioningFeedback
{
[DataMember(Name="header")]
public Header header { get; set; }
[DataMember(Name = "simIdentity")]
public SimIdentity simIdentity { get; set; }
[DataMember(Name = "responseStatus")]
public ResponseStatus responseStatus { get; set; }
[DataMember(Name = "parameters")]
public List<Parameter> parameters { get; set; }
[DataMember(Name = "options")]
public List<Option> options { get; set; }
}
}
I already tried to set an empty namespace on my DataContract but it didn't work.
I'm pretty sure that my names attributes in Datamembers are optionnal here but I desperately tried this.
Can you help me resolve this please ?
Many thanks !

Your DataContract attribute specifies a namespace value "http://my.company.com" and so the xml you get is correct. If you want the datamembers to be in a different (empty) namespace, you could try [DataContract(Namespace = "")]. But I think that the xml you show as what you want it to look is not possible. The child elements do not inherit their parents namespace by default. You could have them inherit their parents default namespace. You xml would then have to look like this:
<root xmlns:blah="...">
<element1 xmlns="whatever">
<element2/>
</element1>
</root>
element2 now inherits element1's namespace

Related

How to use the same libraries in all the classes inside a namespace

I'm implementing various classes in a common folder and namespace all of which need to be able to deserialize JSON, so all the classes need the
using using System.Globalization;
using System.Text.Json.Serialization;
directives. I was wondering if there is a way to make them all use said libraries rather than pasting it into every new class .cs file or writing all the classes in the same .cs file. I've tried putting the using directives inside the namespace but it doesn't carry over to the other .cs files. It might be worth mentioning I'm working on a wpf C# .net framework desktop application.
Sample class without the using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MLaRealERP.Models
{
internal class ClienteP
{
[JsonPropertyName("dni")]
public string DNI { get; set; }
[JsonPropertyName("ruc")]
public string RUC { get; set; }
[JsonPropertyName("nombres")]
public string Nombres { get; set; }
[JsonPropertyName("apellidos")]
public string Apellidos { get; set; }
[JsonPropertyName("celular")]
public string Celular { get; set; }
}
If you're using C# 10 you can use the global modifier on the using directive but this is equivalent to adding it to every file in the project. See the docs here.
If you're on an earlier version, short answer is no.

WCF Data Contracts using class from external assembly

using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using MR.Storage.CommonClasses;
namespace Storage.Contract
{
[DataContract]
public class SaveMyData
{
[DataMember]
public MR.Storage.CommonClasses.MyData MyData{ get; set; }
}
Above is my data contract class for my wcf service. MyData is a poco class in an external assembly that is decorated with [DataContract] and [DataMember] attributes. When I add a service reference to it in a solution I get "Metadata contains a reference that cannot be resolved".
I also tried adding it in wcf test client and it throws error ...\Test Client Projects\14.0\729f94f0-f564-4439-90f9-1c1553821666\Client.cs(42,26) : error CS0234: The type or namespace name 'MyData' does not exist in the namespace 'MR.Storage.CommonClasses' (are you missing an assembly reference?) I opened this file and the only using statement is using System.Runtime.Serialization; Am what I am doing not possible? I saw some other suggestions about using a surrogate, but MyData has a ton of fields so would really like to find a solution that doesnt involve mapping each property
Did you edit the WCF service reference properties? You are able to allude to external assemblies that way. To be honest, I've found that approach to be a pain in the butt (version hell issues), but YMMV.
If you do not necessarily use DataContractSerializer, you could consider using XmlSerializer to serialize your data , it needn't use DataContract and DataMember attribute.
namespace ServiceInterface.Models
{
public class MyData
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class MyDataFather
{
public MyData MyData { get; set; }
}
}
My servicecontract and service. Please don't forget to add XmlSerializerFormat attribute to your service contract, it tells wcf to use XmlSerializer
[ServiceContract]
[XmlSerializerFormat]
public interface ICotractWithoutAttribute
{
[OperationContract]
MyDataFather GetData(MyDataFather myDataFather);
}
public class CotractWithoutAttribute : ICotractWithoutAttribute
{
public MyDataFather GetData(MyDataFather myDataFather)
{
return myDataFather;
}
}
The result.
My client.
using (ChannelFactory<ICotractWithoutAttribute> factory = new ChannelFactory<ICotractWithoutAttribute>("without"))
{
ICotractWithoutAttribute cotractWithoutAttribute = factory.CreateChannel();
ServiceInterface.Models.MyDataFather myDataFather = cotractWithoutAttribute.GetData(new ServiceInterface.Models.MyDataFather { MyData = new ServiceInterface.Models.MyData { Name = "MICK", Age = 18, Id = 1 } });
}
For more information about XmlSerializer, you could refer to https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-the-xmlserializer-class
If you must use DataContractSerializer , I think you should define a class with similar structure with the external class , add DataContarct and DataMember to the class and copy the data of your external class to your own class in your service.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/51b9d122-7345-44c7-8cd8-eb0ccdbffabf/can-i-use-external-assembly-class-in-a-wcf-without-using-poco?forum=wcf

C# EF Required Attribute not recognized

I am creating an app that uses a database (SQLite). I am using entity framework and ADO.NET to interact with it.
I have a seprate Models project in my app that contains all of my Database models.
Now I want to mark some of my class properties as required to reflect the "NOT NULL" option in my database. But if I add the [Required] Attribute from DataAnnotations namespace I get a compiler error saying it cannot be resolved.
Here is how my class looks like :
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ReflowModels
{
public class Tag
{
public Tag()
{
this.Options = new HashSet<Option>();
}
public int Id { get; set; }
[Required]
public string Name { get; set; }
public ICollection<Option> Options { get; set; }
}
}
I have also added a reference to EntityFramework.dll in my project.
you need to add this to your using block
using System.ComponentModel.DataAnnotations.Schema;
you need to add this to your using block
using System.ComponentModel.DataAnnotations;
In case it still doesn't work, maybe you should add it to your References

what 's the use of creating a metadata class and a partial class for the model class c#

I just joined a new company and my manager just joined too, and he wants to change the way we program. basically do what he does. I'm wondering what's the difference, pros, cons, limitation and problems if there'll be any..here's the sample
namespace Models //this is the model part of from edmx
{
using System;
using System.Collections.Generic;
public partial class MyModelClass
{
public int ID { get; set; }
public Nullable<System.DateTime> PostDate { get; set; }
public string MyContent { get; set; }
}
}
this is the metadata:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Models
{
public class MyModelMetaData
{
//he wants all data annotation here
public int ID { get; set; }
public Nullable<System.DateTime> PostDate { get; set; }
public string MyContent { get; set; }
}
}
this is the partial:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Models
{
[MetadataType(typeof(MyModelMetaData))]
public partial class MyModelClass or MyModelClassPartial
{
//He wants the programming algorithm to go here
}
}
Please enlightened me. and he wants to create different metadata and partial classes per model class..way too many files involved.
thank you..i need an answer as to why..if you think his method is good..I will do this..but if you think this will cause problem in the future and more coding will be involve..i need to know
The first class you show, the entity classes, are generated from the database every time you save the EDMX (or when you execute the T4 Template).
This causes the file containing public partial class MyClass under the EDMX to be regenerated. So you cannot alter it, because the next time someone refreshes a table or adds one, your changes are gone.
That's why entity classes are generated as a partial: so you can create another partial to the same class to do your modifications in.
However, if you want to annotate your entity's properties with metadata, you cannot redefine the same property in the other partial class: the same name can only be used by one member of a type. So you can't do this:
// Entity class
public partial class FooEntity
{
public string Name { get; set;}
}
// User-created partial class
public partial class FooEntity
{
[Required]
public string Name { get; set;}
}
Because that code expresses you want two properties named Name in the FooEntity class, which is not valid.
So you'll have to come up with another way to add metadata to the type. Enter the [MetadataType] attribute. This works by creating a new class with the same properties as the class to be annotated. Here, using reflection, the metadata is resolved based on member name.
So when you create a metadata class for the above annotation:
public class FooEntityMetadata
{
[Required]
public string Name { get; set;}
}
You can apply it to the user-created partial:
// User-created partial class
[MetadataType(typeof(FooEntityMetadata))]
public partial class FooEntity
{
}
And also, in the latter partial, you can add members that add functionality to the entity model. New ([NotMapped]) properties and new methods for example.
I think the one use could be to not pollute the main class.
For example if you have a lot of attribute for validation (using dataannotation) and you don't want to have them in the main class you could use the MetadataTypeAttribute for that.
Another use could be if your class is auto-generated and you need to add some decoration (more attributes) to your properties without changing the autogenerated code.

TFS allowing me to access just one class out of five classes in the same folder in .net

I'm very new to working with TFS(of course not with Visual Studio). I have 6 projects in same solution and one of the projects has a folder with 6 c-sharp classes in it. When i try to access these classes from inside of other projects ( which all are in the same solution) except one class other five classes don't appear in the intellisense.
I have manually added reference to this project in other project. Which is why I'm able to access one class but my confusion is why not other classes are accessible through intellisense.
Did I do something wrong. I did check-in whatever pending changes were there in the entire solution still no luck.
Your help will be highly appreciated
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NS.GIS.PTC.Core.Entities;
namespace NS.GIS.PTC.WebServices.DataManagement.Model.Dtos
{
public class FieldDescriptionDto
{
public string Name
{
get;
set;
}
public string Type
{
get;
set;
}
public string Alias
{
get;
set;
}
public int Length
{
get;
set;
}
public bool IsSystem
{
get;
set;
}
public Domain Domain
{
get;
set;
}
}
}
here is the code and below is the code that is using it I have written it forcibly coz as u know its not appearing in intellisense
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NS.GIS.PTC.Core.Entities;
using NS.GIS.PTC.WebService.DataManagement.Model.Dtos;
namespace NS.GIS.PTC.WebService.DataManagement.API.Model
{
internal static class FieldDescriptionExtension
{
internal static FieldDescriptionDto ToFieldDescriptionDto(this FieldDescription fieldDescription)
{
return new FieldDescriptionDto
{
Name = fieldDescription.Name,
Type = fieldDescription.Type,
Alias= fieldDescription.Alias,
};
}
}
}
Please check if all the classes are marked Public.
Check if their namespaces have been included.
Restart VS and try.
Forcibly use them and see if you get a squiggle to auto-include namespace etc.

Categories