I have built a Class Library (Portable for iOS, Android and Windows) in VS2015.
I created a public class that having a data type with DataTable.
public class PNAWcfData
{
public int CmdTimeout { get; set; }
public string ErrMsg { get; set; }
public DataTable DTResult { get; set; }
public string XmlResult { get; set; }
}
However, I got error when build the solution.
“The type or namespace name 'DataTable' could not be found (are you
missing a using directive or an assembly reference?)”
I tried to add the using System.Data but still getting the same error. Also tried to add from Project->Reference, but it is telling that component is already automatically referenced by the build system.
Any idea why I am getting this error?
DataTable classes are not available in the PCL. This is a duplicate of Can you use DataSet and DataTables in a Portable Class Library
If you are using Xamarin, you may consider switching to Shared Project instead of PCL.
Related
trying out Xamarin for the first time and can't really figure this one out.
I am working on a solution that contains 3 projects. One .NET standard 2 class library with Entity Framework, that I use as a service layer, which the other projects reference and calls to get its data objects.
The other two projects are a .NET Core 2 Web project and a Xamarin.Android project.
The .NET Core 2 project can retrieve all data without any issues, while the Xamarin.Android project get InvalidCastException when an object contains a DateTime property.
System.InvalidOperationException: An exception occurred while reading a database value for property 'myObject.LastLogin'. The expected type was 'System.DateTime' but the actual value was of type 'System.String'
I am using the same methods and classes for both the Xamarin.Android project and the .NET Core 2 project. I also tried returning the object from the service layer with the DateTime property set to DateTime.Now always, to ensure it's a proper date.
Same object works fine on Xamarin.Android if I remove the DateTime property. Example of the object returned below.
public class User
{
public int ID { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime LastLogin { get; set; }
public byte[] ProfilePicture { get; set; }
}
If anyone have any ideas to how I can fix this I would really appreciate it.
I am working around it currently, but would really like to have a proper solution to this issue.
Thanks!
I am in charge of migrating our companies core libraries from Compact Framework 2.0 that used to run on Visual Studio 2008 to Visual Studio 2015, Update 3 and .Net 4.6. I have created new class libraries and have changed the references to point to the corresponding .Net version of the older libraries. For example, if we had a ProdoctCore library that was referencing Sender.dll and Utils.dll, in the new ProducCoreDotNet library, I have added its references to SenderDotNet.dll and UtilsDotNet.dll. Then I have added the class files of the old Compact Framework to the new solution by adding them "as link". So basically the project names have the extention DotNet but the namespaces have exactly the same name is before.
Now the problem that I'm facing is that I'm getting a strange error:
"The reference type 'IMonitorComponent' is defined in an assembly that is not referenced. You must add a reference to assembly 'Utils, Version=1.5.0.0, Culture=neutral, PublicKeyToken=null'"
However, my UtilsDotNet's version is 1.0.0.0 and the old Utils was version 2.0.0.0.
I've read couple of similar threads at SO but unlike what was mentioned in this question, the IMonitorComponent doesn't have any reference to other assemblies. It's simply an interfaces with couple of properties:
public enum COMPONENT_STATUS
{
ERROR,
WARNING,
OK,
UNKNOWN,
DISABLED
}
public class ComponentStatusProperty
{
public ComponentStatusProperty(string name, COMPONENT_STATUS status, string message)
{
ComponentName = name;
Status = status;
Message = message;
}
public COMPONENT_STATUS Status { get; set; }
public string Message { get; set; }
public string ComponentName { get; set; }
}
public interface IMonitorComponent
{
string Name { get; }
List<ComponentStatusProperty> Statuses { get; }
bool ComponentSoBrokenThatTheDeviceCannotWork { get; }
}
So I'm out of ideas and would appreciate your help. Also, please elaborate on you answers as I haven't done anything like this before.
We finally found the reason. We were using another reference that was using the Utils version 1.5. So I created another class library for that reference and used the new UtilsDotNet as its reference and problem was fixed.
I have a Entity class (auto-generated) that looks like this:
namespace FicServerData
{
using System;
using System.Collections.Generic;
public partial class Snapshot
{
public Snapshot()
{
this.ComponentQuotes = new HashSet<SnapshotPart>();
}
public int Id { get; set; }
public System.DateTime Time { get; set; }
public string Machine { get; set; }
public string AppName { get; set; }
public virtual ICollection<SnapshotPart> ComponentQuotes { get; set; }
}
}
Then I wrote a utility partial class to looks like this:
namespace FicServerData
{
public partial class Snapshot
{
public IEnumerable<Quote> DerivedQuotes
{
get
{
...
}
}
}
}
So those are both files in the same project and as you can see in the same namespace. Now inside this project I can access the property i added no problem. I can't access it from a project that references it though: i only have access to the Entity class VS has created for me.
What am I missing here?
You cannot access partial classes across projects. The 'parts' of partial classes are compiled to a single class within a single assembly.
The fix for me was to restart the Visual Studio. I am at version Professional 2017.
I had this problem. I found that my consuming project was referencing the source project via an assembly reference rather than a project reference. Further, the assembly being referenced was stored in a drive folder, and that drive folder was added as a reference path in the consuming project. As you would expect, my updates in the source project were compiling, but my referenced assembly was not. I deleted the reference in my consuming project and re-added it as a project reference, et voila! Problem solved!
Don't ask me why my consuming project was using a refence path to access a compiled assembly from another project in the same solution. Please, don't.
I was trying to implement parsing a JSON response as shown here for my Windows Phone 7 project in C#. But I am stuck with a compilation error as "The type or namespace name 'Serializable' could not be found (are you missing a using directive or an assembly reference?)"
I have the imports using System.Runtime.Serialization;
using System.Runtime.Serialization.Json; I am not sure what are import I am missing. I tried to include using System.ServiceModel.Web; But the Web part is not recognized.
I thought my project is not pointing to the right framework from here. But in the Assembly information, there is no option for me to change the target framework.
This looks like a similar problem to mine, but I couldn't find the JSON.NET in .net dlls which is filtered for Windows Phone.
Can someone help me to get this JSON thing working for Windows Phone 7.
Thank in Advance.
EDIT - 7/3/11
My Jason response is
{ "serviceresponse" : { "servicename" : "RequestRegisterUser", .....
And my Response objects are:
[DataContract]
public class serviceresponse
{
[DataMember]
public String servicename { get; set; }
.
.
.
And my Deserialize method:
public static T Deserialise<T>(string json)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
obj = (T)serializer.ReadObject(stream);
return obj;
}
}
Now I am getting this error after Deserializing the response:
servicename Could not evaluate expression string
( I could not import System.ServiceModel.Web though I have the dll in the reference. A compilation error on the .Web part (The type or namespace name 'Web' does not exist in the namespace 'System.ServiceModel') )
EDIT After more research, I found my response when viewed in the debugger is actually
{
\"serviceresponse\": {
\"servicename\": \"RequestRegisterUser\",.....
I searched for this and found this could be a problem. How can I format it to correct JSON String??
You need to add a reference to both System.Runtime.Serialization and System.ServiceModel.Web assemblies. The DataContractJsonSerializer is defined in System.ServiceModel.Web assembly in the Silverlight version of the framework, that's why you need the extra assembly reference.
And by the way JSON.NET is a a popular open-source JSON framework for .Net and you could find more about it here. It's not part of the .Net framework, that's why you can't find it.
Edit:
About the compilation, in Silverlight the DataContractJsonSerializer is in the System.Runtime.Serialization.Json namespace, but in the assembly System.ServiceModel.Web (in System.ServiceModel.Web.dll), which is a bit confusing. So you use it like this - System.Runtime.Serialization.Json.DataContractJsonSerializer, but need the extra assembly reference. You also need to reference the System.Runtime.Serialization assembly as well, because that is where the DataContract attribute is defined. I see you have already successfully compiled the code, but I hope the extra explanation makes it more clear for future readers.
About the serialization itself - as you have already found out, you will need two objects, simply because that's the structure of the json. However, the DataContract and DataMember attributes have a Name property that you can use instead of changing the name of the fields. Also, you can use properties instead of fields if you like.
For example:
[DataContract]
public class ServiceResponse
{
[DataMember(Name = "servicename")]
public string ServiceName { get; set; }
}
[DataContract]
class Response
{
[DataMember(Name = "serviceresponse")]
public ServiceResponse ServiceResponse { get; set; }
}
And one last thing - you don't need the call to Activator.CreateInstance(); in your Deserialise method.
It certainly would help if you posted your code. So I can only guess:
I assume you have something like this:
[Serializable]
public class Response
{
[DataMember]
public string name { get; set; }
...
}
But that's a mix-up of two serialization concepts, one of which is not supported in Phone 7. The correct attributes are DataContract and DataMember:
[DataContract]
public class Response
{
[DataMember]
public string name { get; set; }
...
}
I found the issue. Though my class name is "serviceresponse", I used another wrapper class as
public class Response
{
public serviceresponse serviceres;//Don't Do this....
}
where I used the variable name for serviceresponse as serviceres. But when I changed it to " serviceresponse" its all working.
public class Response
{
public serviceresponse serviceresponse;//This fixed it.
}
I have a Silverlight solution that references a third-party web service. This web service generates XML, which is then processed into objects for use in Silverlight binding. At one point we the processing of XML to objects was done client-side, but we ran into performance issues and decided to move this processing to the proxies in the hosting web project to improve performance (which it did). This is obviously a gross over-simplification, but should work. My basic project structure looks like this.
Solution
Solution.Web - Holds the web page
that hosts Silverlight as well as
proxies that access web services and
processes as required and obviously
the references to those web
services).
Solution.Infrastructure - Holds
references to the proxy web services
in the .Web project, all genned code
from serialized objects from those
proxies and code around those objects
that need to be client-side.
Solution.Book - The particular
project that uses the objects in
question after processed down into
Infrastructure.
I've defined the following Interface and Class in the Web project. They represent the type of objects that the XML from the original third-party gets transformed into and since this is the only project in the Silverlight app that is actually server-side, that was the place to define and use them.
//Doesn't get much simpler than this.
public interface INavigable
{
string Description { get; set; }
}
//Very simple class too
public class IndexEntry : INavigable
{
public List<IndexCM> CMItems { get; set; }
public string CPTCode { get; set; }
public string DefinitionOfAbbreviations { get; set; }
public string Description { get; set; }
public string EtiologyCode { get; set; }
public bool HighScore { get; set; }
public IndexToTabularCommandArguments IndexToTabularCommandArgument { get; set; }
public bool IsExpanded { get; set; }
public string ManifestationCode { get; set; }
public string MorphologyCode { get; set; }
public List<TextItem> NonEssentialModifiersAndQualifyingText { get; set; }
public string OtherItalics { get; set; }
public IndexEntry Parent { get; set; }
public int Score { get; set; }
public string SeeAlsoReference { get; set; }
public string SeeReference { get; set; }
public List<IndexEntry> SubEntries { get; set; }
public int Words { get; set; }
}
Again; both of these items are defined in the Web project. Notice that IndexEntry implments INavigable. When the code for IndexEntry is auto-genned in the Infrastructure project, the definition of the class does not include the implmentation of INavigable. After discovering this, I thought "no problem, I'll create another partial class file reiterating the implmentation". Unfortunately (I'm guessing because it isn't being serialized), that interface isn't recognized in the Infrastructure project, so I can't simply do that. Here's where it gets really weird. The BOOK project CAN see the INavigable interface. In fact I use it in Book, though Book has no reference to the Web Service in the Web project where the thing is define, though Infrastructure does. Just as a test, I linked to the INavigable source file from indside the Infrastructure project. That allowed me to reference it in that project and compile, but causes havoc in the Book project, because now there's a conflick between the one define in Infrastructure and the one defined in the Web project's web service. This is behavior I would expect.
So, to try and sum up a bit. Web project has a web service that process data from a third-party service and has a class and interface defined in it. The class implements the interface. The Infrastructure project references the web service in the Web Project and the Book project references the Infrastructure project. The implmentation of the interface in the class does NOT serialize down, so the auto-genned code in INfrastructure does not show this relationship, breaking code further down-stream. The Book project, whihc is further down-stream CAN see the interface as defined in the Web Project, even though its only reference is through the Infrastructure project; whihc CAN'T see it.
Am I simple missing something easy here? Can I apply an attribute to either the Interface definition or to the its implmentation in the class to ensure its visibility downstream? Anything else I can do here?
I know this is a bit convoluted and anyone still with me here, thanks for your patience and any advice you might have.
Cheers,
Steve