Answered by:
Access Modifier error.

Question
-
User1421620300 posted
This is the controller code for a .svc file. It is not apparent as to why i receive the following error! I believe that it states their is something wrong with type or format of public access modifier for this version state supposedly.
Link: https://ibb.co/znTV0dH
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "Fight/{Id1}/{Id2}", Method = "GET")]public string Fight(string Id1, string Id2) //error code! (public)
{
SuperHero hero1 = Data.SuperHeroes.Find(hero => hero.Id == int.Parse(Id1));
SuperHero hero2 = Data.SuperHeroes.Find(hero => hero.Id == int.Parse(Id2));if (hero1.Combat > hero2.Combat)
{
return $"{hero1.HeroName} wins!";
}if (hero2.Combat > hero1.Combat)
{
return $"{hero2.HeroName} wins!";
}
return "its a tie!";}
}
}Severity Code Description Project File Line Suppression State
Error CS0106 The modifier 'public' is not valid for this item SuperHeroDB C:\Users\Andrew\Documents\Visual Studio 2015\MyProjects\SuperHeroDB\SuperHeroDB\Service\SuperHeroService.svc.cs 140 Active
Error CS8652 The feature 'local function attributes' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.SuperHeroDB C:\Users\Andrew\Documents\Visual Studio 2015\MyProjects\SuperHeroDB\SuperHeroDB\Service\SuperHeroService.svc.cs 136 Active
Monday, May 4, 2020 7:34 PM
Answers
-
User475983607 posted
You did not share all the relevant code so I'm guessing. Operational contracts are Interfaces which do not have access modifiers or a body of code. The implementation of the Interface has these features. An Operation contract has the following pattern and within an Interface
[ServiceContract] public interface IService1 { [OperationContract] string Fight(string Id1, string Id2); }
The implantation of the Interface has method body.
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "Fight/{Id1}/{Id2}", Method = "GET")] public string Fight(string Id1, string Id2) //error code! (public) { SuperHero hero1 = Data.SuperHeroes.Find(hero => hero.Id == int.Parse(Id1)); SuperHero hero2 = Data.SuperHeroes.Find(hero => hero.Id == int.Parse(Id2)); if (hero1.Combat > hero2.Combat) { return $"{hero1.HeroName} wins!"; } if (hero2.Combat > hero1.Combat) { return $"{hero2.HeroName} wins!"; } return "its a tie!"; }
You've been on these forum for a long time.... Please use the "Insert/Edit Code samples" button when posting code on the forms.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 4, 2020 7:58 PM
All replies
-
User475983607 posted
You did not share all the relevant code so I'm guessing. Operational contracts are Interfaces which do not have access modifiers or a body of code. The implementation of the Interface has these features. An Operation contract has the following pattern and within an Interface
[ServiceContract] public interface IService1 { [OperationContract] string Fight(string Id1, string Id2); }
The implantation of the Interface has method body.
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "Fight/{Id1}/{Id2}", Method = "GET")] public string Fight(string Id1, string Id2) //error code! (public) { SuperHero hero1 = Data.SuperHeroes.Find(hero => hero.Id == int.Parse(Id1)); SuperHero hero2 = Data.SuperHeroes.Find(hero => hero.Id == int.Parse(Id2)); if (hero1.Combat > hero2.Combat) { return $"{hero1.HeroName} wins!"; } if (hero2.Combat > hero1.Combat) { return $"{hero2.HeroName} wins!"; } return "its a tie!"; }
You've been on these forum for a long time.... Please use the "Insert/Edit Code samples" button when posting code on the forms.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 4, 2020 7:58 PM -
User1421620300 posted
I corrected error mgebhard, it was missing brackets in Controller class.. It was not apparent to me as i am following tutorial.
Friday, May 8, 2020 12:46 AM