C# dll System.Reflection.TargetInvocationException error - c#

I'm new on vs C#. I want to create dll file too use meta trader 5. my dll correct working in visual studio. But not working in meta trader.
Problem is Newtonsoft.Json packages.
Code
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Analiz
{
public class anlz
{
public static void metot()
{
var myDetails = JsonConvert.DeserializeObject<List<MyDetail>>(File.ReadAllText(#"C:\Users\Durak\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\Files\json\deneme.json"));
}
}
public class MyDetail
{
public string emirtipi{get;set;}
public string miktar{get;set;}
public string takip{ get; set;}
}
}
Error :
enter image description here

When creating a DLL for an external program which does not support Newtonsoft.Json, you cannot build it this way.
Remove the reference to Newtonsoft.Json from your program.
Add a reference to System.Runtime.Serialization
After that you can refactor your code to:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Analiz
{
public class anlz
{
public static void metot()
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Test>));
List<MyDetail> myDetails = (List<Test>)serializer.ReadObject(new FileStream(#"C:\Users\Durak\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\Files\json\deneme.json", FileMode.Open, FileAccess.Read));
}
}
public class MyDetail
{
public string emirtipi{get;set;}
public string miktar{get;set;}
public string takip{ get; set;}
}
}

Related

'StaticClass' does not contain a definition for 'Property'

Edit 2, I'll Rephrase the whole post:
I have the static class Project.Shared.Constants and it has some constants and public getter properties:
using System;
using System.IO;
using Microsoft.Win32;
namespace Project.Shared
{
public static class Constants
{
public const RegistryHive HIVE = RegistryHive.LocalMachine;
/*
... more constants
*/
public static RegistryView RegistryView
{
get
{
return Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
}
}
}
}
Then I had a Project.Infrastructure.RegistryReader. It exists in the code for quite some time and uses the static class.
using System;
using System.IO;
using Microsoft.Win32;
using Project.Shared;
namespace Project.Infrastructure
{
public class RegistryReader
{
public string Read(string keyName)
{
using var baseKey = RegistryKey.OpenBaseKey(Constants.HIVE, Constants.RegistryView);
using var subRegistryKey = baseKey.OpenSubKey(keyName);
/*
Actual access, read, parse
*/
return resultValue;
}
}
}
Now I want to add a Project.Infrastructure.RegistryWriter in the same project and same namespace and use the same statics:
using System;
using System.IO;
using Microsoft.Win32;
using Project.Shared;
namespace Project.Infrastructure
{
public class RegistryWriter
{
public void WriteEthernetChannelInRegistry(string keyName, string value)
{
using var baseKey = RegistryKey.OpenBaseKey(Constants.HIVE, Constants.RegistryView);
using var subRegistryKey = baseKey.OpenSubKey(keyName, writable: true);
/*
Actual writing of the registry key
*/
}
}
}
Locally it works, so it can't be totally wrong. On the build system it fails, but only with the Writer. If I declare the same property locally, then the build finishes and has no issues with the Reader.
It might be something between VS using msbuild and the build system using dotnet build --Release but I couldn't make it fail locally yet.
Edit 3, the error I receive is:
Error: RegistryWriter.cs(72,99): error CS0117: 'Constants' does not contain a definition for 'RegistryView' [c:\source\...\Project.Infrastructure.csproj]

SolrNet in C# example

I am trying to write a simple c# program to just connect to my Solr instance. I have downloaded the SolrNet library and made reference to SolrNet.dll in my project. I am following some of the examples online but have been getting the error "ServiceLocationProvider must be set" when the last line of my code is executed. Following is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using CommonServiceLocator;
using SolrNet;
using SolrNet.Attributes;
using SolrNet.Impl;
using SolrNet.Impl.DocumentPropertyVisitors;
using SolrNet.Impl.FacetQuerySerializers;
using SolrNet.Impl.FieldParsers;
using SolrNet.Impl.FieldSerializers;
using SolrNet.Impl.QuerySerializers;
using SolrNet.Impl.ResponseParsers;
using SolrNet.Mapping;
using SolrNet.Mapping.Validation;
using SolrNet.Mapping.Validation.Rules;
using SolrNet.Schema;
using SolrNet.Utils;
namespace WebApplication1
{
public class Post
{
[SolrField("id")]
public string id { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
var connection = new SolrConnection("http://localhost:8983/solr/test");
Startup.Init<Post>(connection.ServerURL);
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Post>>();
}
}
}
Can someone tell me what am I missing?
Thank You

ApplicationContext.Current is coming null

I found same question in stack over flow as here and tried same think but still I am receiving ApplicationContext.Current as null.
I am making the web service where I need to pull out a couple of pieces of data from an Umbraco database. I don't need any of the Umbraco views or any of that stuff. I'm new to the Umbraco Core libraries.
What I did was get a below reference to my new web service project
umbraco.dll
Umbraco.Core.dll
umbraco.DataLayer.dll
umbraco.editorControls.dll
umbraco.MacroEngines.dll
umbraco.providers.dll
Umbraco.Web.UI.dll
umbraco.XmlSerializers.dll
UmbracoExamine.dll
And the below class code where I am receiving ApplicationContext.Current as null
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Web.Mvc;
using umbraco.MacroEngines;
using Umbraco.Web;
using Umbraco.Web.WebApi;
using umbraco.NodeFactory;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using umbraco.cms.businesslogic.media;
using umbraco.BusinessLogic;
using System.Configuration;
using log4net;
using System.Reflection;
using Umbraco.Core.Logging;
using System.Web.Optimization;
using System.Web.Http;
using System.Data.SqlClient;
using System.Data;
using System.Web.Script.Serialization;
using System.Collections;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using Stripe;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Web.Configuration;
using System.Web.Security;
using Newtonsoft.Json;
using System.Net.Mail;
using System.Text;
using System.Net;
using System.IO;
public class RegisterUserController : UmbracoApiController
{
public static string UmbracoConnectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;
public static IMemberService memberService = ApplicationContext.Current.Services.MemberService;// Here I am getting ApplicationContext.Current as null
}
But I am calling this class I am getting ApplicationContext.Current as null.
So because of that I have Many methods in this class which works on memberService and due to this null reference they are not working. Even other methods which are not using member service are not being called.
For me, the code below seems to work.
public class TestApiController : UmbracoApiController
{
private static string _umbracoConnectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;
private static IMemberService _memberService = global::Umbraco.Core.ApplicationContext.Current.Services.MemberService;
public int GetTest()
{
var memberCount = _memberService.Count();
return memberCount;
}
}
If the above code isn't working for you e.g. with AJAX, try the below code:
public class TestApiController : UmbracoApiController
{
public int GetTest()
{
var ms = Services.MemberService;
return ms.Count();
}
}

Entity Framework Windows App

I have EF application with following code.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormswithEFSample
{
public class ObservableListSource<T> : ObservableCollection<T>, IListSource
where T : class
{
private IBindingList _bindingList;
bool IListSource.ContainsListCollection { get { return false; } }
IList IListSource.GetList()
{
return _bindingList ?? (_bindingList = this.ToBindingList());
}
}
}
But when build the project attached error comming. What is the error in this code?
Try add namespace System.Data.Entity which resides in EntityFramework.dll library (this assembly is necessery).

Custom Class Inside a WCF Service Object

A bit rusty here with regards to WCF Services.
I have a custom class named cSecurity.cs which does some custom functions.
I want to use this custom class inside my Service:
This is the App.svc.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace AppServices
{
public class App : IApp
{
public cSecurity _csec;
public string GetItems(int agentID, string agentName)
{
// Need to use some functions from the cSecurity class here???
return _csec.getItems();
}
}
}
This is the cSecurity.cs class:
using System.Collections;
using System.Configuration;
using System.Net;
namespace AppServices
{
public class cSecurity
{
// Some functions defined here....
public string getItems(){
return string.Empty;
}
}
}
But I am getting an error on the line:
public cSecurity _csec;
"The type or namespace name 'cSecurity' could not be found."
This seems pretty trivial but I seem to be lost here.
Appreciate any insight. Thanks.
For some reason, the solution for this was for me to close and restart VS. Not sure what caused the class for it to be not referenced by VS.

Categories